fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5.  
  6. #define ENABLE_LCS
  7. #define ENABLE_LIS
  8. #define ENABLE_MAIN
  9.  
  10. #ifdef ENABLE_LCS
  11. namespace lcs {
  12. constexpr auto N = 50, M = 50;
  13. int dp[N + 1][M + 1];
  14.  
  15. template <typename Iterable> int lcs(Iterable a, Iterable b) {
  16. memset(dp, sizeof(dp), 0);
  17. auto n = a.size();
  18. auto m = b.size();
  19. for (auto i = 0; i < n; ++i) for (auto j = 0; j < m; ++j) {
  20. dp[i + 1][j + 1] = a[i] == b[j] ? dp[i][j] + 1 : std::max(dp[i][j + 1], dp[i + 1][j]);
  21. }
  22. return dp[n][m];
  23. }
  24. }
  25. #endif
  26.  
  27. #ifdef ENABLE_LIS
  28. namespace lis {
  29. constexpr auto N = 50;
  30. int INF = 2000000001;
  31. int dp[N];
  32.  
  33. int lis(std::vector<int> a) {
  34. auto n = a.size();
  35. for (auto &i: dp) i = INF;
  36. for (auto i = 0; i < n; ++i) {
  37. *std::lower_bound(dp, dp + N, a[i]) = a[i];
  38. }
  39. return std::lower_bound(dp, dp + N, INF) - dp;
  40. }
  41. }
  42. #endif
  43.  
  44. #ifdef ENABLE_MAIN
  45. int main() {
  46. std::string x = "XMJYAUZ", y = "MZJAWXU";
  47. std::cout << lcs::lcs(x, y) << " should be 4" << std::endl;
  48.  
  49. std::vector<int> a = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
  50. std::cout << lis::lis(a) << " should be 6" << std::endl;
  51. return 0;
  52. }
  53. #endif
Success #stdin #stdout 0s 3484KB
stdin
Standard input is empty
stdout
4 should be 4
6 should be 6