fork download
  1. #include<bits/stdc++.h>
  2. #define M 1004
  3. using namespace std;
  4.  
  5. string a, b;
  6. int dp[M][M];
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0); cout.tie(0);
  12.  
  13. getline(cin, a);
  14. getline(cin, b);
  15. int n = a.size();
  16. int m = b.size();
  17. a = ' ' + a;
  18. b = ' ' + b;
  19. for(int i = 1; i <= n; i++)
  20. {
  21. for(int j = 1; j <= m; j++)
  22. {
  23. if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
  24. else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  25. }
  26. }
  27. cout << dp[n][m];
  28. }
  29.  
Success #stdin #stdout 0.01s 5292KB
stdin
abc1def2ghi3
abcdefghi123
stdout
10