fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int MAX = 3001;
  6. int P[MAX][MAX] = {0};
  7.  
  8. int max_2(int a, int b) {
  9. return (a > b) ? a : b;
  10. }
  11.  
  12. int main() {
  13. string A, B;
  14. cin >> A >> B;
  15.  
  16. int a = A.size();
  17. int b = B.size();
  18.  
  19. for (int i = 1; i <= a; i++) {
  20. for (int j = 1; j <= b; j++) {
  21. if (A[i - 1] == B[j - 1]) {
  22. P[i][j] = P[i - 1][j - 1] + 1;
  23. } else {
  24. P[i][j] = max_2(P[i - 1][j], P[i][j - 1]);
  25. }
  26. }
  27. }
  28.  
  29. cout << P[a][b] << "\n";
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5324KB
stdin
barka brak
stdout
3