Maximum increment subsequence, longest common substring, longest common subsequence, string edit distance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.