2014-07-11 13:18:53
题意&思路:水水的动规,要注意gets输入,因为字符串中可能有空格。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int main(){ 6 int tmax,len1,len2,dp[1005][1005]; 7 char s1[1005],s2[1005]; 8 while(gets(s1 + 1)){ 9 gets(s2 + 1); 10 tmax = 0; 11 len1 = strlen(s1 + 1); 12 len2 = strlen(s2 + 1); 13 memset(dp,0,sizeof(dp)); 14 for(int i = 1; i <= len1; ++i) 15 for(int j = 1; j <= len2; ++j){ 16 if(s1[i] == s2[j]) 17 dp[i][j] = dp[i - 1][j - 1] + 1; 18 else 19 dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]); 20 } 21 printf("%d\n",dp[len1][len2]); 22 } 23 return 0; 24 }
本文介绍了一种使用动态规划解决字符串匹配问题的方法。通过分析两个字符串之间的最长公共子序列,该程序能够找出两个字符串之间的最大匹配长度。采用C++实现,并注意到了字符串可能包含空格的情况。
465

被折叠的 条评论
为什么被折叠?



