Http://www.cnblogs.com/zhangchaoyang/articles/2012070.html
Convert a problem to a number of smaller sub-problems, and all rely on a two-dimensional matrix to achieve the calculation.
Convention: The string S is stripped of the last character T after S ', T1 and T2 are the last characters of S1 and S2 respectively.
Then Dist (S1,S2) is the smallest of the following 4 values:
1.dist (S1 ', S2 ')--when T1==T2
2.1+dist (S1 ', S2)--when t1!=t2, and delete the last character S1
3.1+dist (S1,S2 ')--when t1!=t2, and add a character after S1 T2
4.1+dist (S1 ', S2 ')--when t1!=t2, and change S1 's most character T1 to T2
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < Cstring> #include <queue>using namespace std;const int maxn=500;const int inf=0x3f3f3f3f;const int chart=26; typedef unsigned long long ll;int Dp[maxn][maxn];char s1[500],s2[500];int main () { int m,n; while (scanf ("%s%s", S1,s2)!=eof) { n=strlen (S1); M=strlen (S2); Memset (Dp,0x3f,sizeof (DP)); for (int i = 0;i<=max (m,n); i++) dp[0][i]=dp[i][0]=i; for (int i = 1;i<=n;i++) for (int j = 1;j<=m;j++) { if (s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]; Dp[i][j]=min (Dp[i][j],min (dp[i-1][j]+1,dp[i][j-1]+1)); Dp[i][j]=min (dp[i][j],dp[i-1][j-1]+1); } printf ("%d\n", Dp[n][m]); } return 0;}
The longest common child string
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < Cstring> #include <queue>using namespace std;const int maxn=500;const int inf=0x3f3f3f3f;const int chart=26; typedef unsigned long long ll;int Dp[maxn];char s1[500],s2[500];int main () { int m,n; while (scanf ("%s%s", S1,s2)!=eof) { int ans=0; N=strlen (S1); M=strlen (S2); Memset (Dp,0,sizeof (DP)); for (int i = 1;i<=n;i++) for (int j = m;j;j--) { if (s1[i-1]==s2[j-1]) dp[j]=dp[j-1]+1; else dp[j]=0; Ans=max (Ans,dp[j]); } printf ("%d\n", ans); } return 0;}
Maximum increment subsequence, longest common substring, longest common subsequence, string edit distance