1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <iostream> #include <cstdio> #include <string.h> #include <algorithm> using namespace std;
char s1[1000001], s2[1000001]; int dp[2][1000005];
int lcs() { int len1 = strlen(s1), len2 = strlen(s2); int i, j, cur = 1, pre = 0, cnt = 0; if(len2 > len1) { for(i = 0; i <= len2; i++) dp[0][i] = 0; dp[1][0] = 0; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { if(s1[i - 1] == s2[j - 1]) { dp[cur][j] = dp[pre][j - 1] + 1; cnt = max(cnt, dp[cur][j]); } else dp[cur][j] = 0; } swap(cur, pre); } return cnt; } else { for(i = 0; i <= len1; i++) dp[0][i] = 0; dp[1][0] = 0; for(i = 1; i <= len2; i++) { for(j = 1; j <= len1; j++) { if(s2[i - 1] == s1[j - 1]) { dp[cur][j] = dp[pre][j - 1] + 1; cnt = max(cnt, dp[cur][j]); } else dp[cur][j] = 0; } swap(cur, pre); } return cnt; } }
int main() { while(~scanf("%s%s", s1, s2)) { int ans = lcs(); printf("%d\n", ans); } return 0; }
|