1 //Accepted 4112 KB 16 ms 2 //最长公共子串 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 1005; 8 int dp[imax_n][imax_n]; 9 char s1[imax_n]; 10 char s2[imax_n]; 11 int len1,len2; 12 int max(int a,int b) 13 { 14 return a>b?a:b; 15 } 16 void Dp() 17 { 18 memset(dp,0,sizeof(dp)); 19 for (int i=1;i<=len1;i++) 20 { 21 for (int j=1;j<=len2;j++) 22 { 23 dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 24 if (s1[i-1]==s2[j-1]) 25 dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); 26 } 27 } 28 printf("%d\n",dp[len1][len2]); 29 } 30 int main() 31 { 32 while (scanf("%s%s",s1,s2)!=EOF) 33 { 34 len1=strlen(s1); 35 len2=strlen(s2); 36 Dp(); 37 } 38 return 0; 39 }
时间: 2024-10-28 09:58:54