最长公共子序列(LCS)

1.求LCS长度
输入:
abcfbc abfcab
programming contest
abcd mnp
输出:

4
2
0


#include 
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
 
char a[1001], b[1001];
int dp[1001][1001], len1, len2;
 
void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else if(dp[i-1][j] > dp[i][j-1])
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = dp[i][j-1];
        }
    }
}
 
int main()
{
    while(~scanf(" %s",a))
    {
        scanf(" %s", b);
        memset(dp, 0, sizeof(dp));
        len1 = strlen(a);
        len2 = strlen(b);
        lcs(len1, len2);
        printf("%d\n", dp[len1][len2]);
    }
    return 0;
}

2.求LCS是什么
输入:
abcicba
abdkscab
输出:
abca


#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
 
using namespace std;
 
char a[1001], b[1001];
int dp[1001][1001], len1, len2;
 
void lcs(int i, int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else if(dp[i-1][j] > dp[i][j-1])
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = dp[i][j-1];
        }
    }
}
 
void llcs()
{
    int i, j, z = 0;
    char c[1001];
    memset(c, 0, sizeof(c));
    i = len1, j = len2;
    while(i!=0 && j!=0)
    {
        if(a[i-1] == b[j-1])
        {
            c[z++] = a[--i];
            j--;
        }
        else if(dp[i-1][j] < dp[i][j-1])
            j--;
        else if(dp[i][j-1] <= dp[i-1][j])
            i--;
    }
    for(i=z-1; i>=0; i--)
        printf("%c", c[i]);
    printf("\n");
 
}
 
int main()
{
    while(~scanf(" %s", a))
    {
        scanf(" %s", b);
        memset(dp, 0, sizeof(dp));
        len1 = strlen(a);
        len2 = strlen(b);
        lcs(len1, len2);
        llcs();
    }
    return 0;
}

3.根据LCS将两个词拼接
输入:
apple peach
ananas banana
pear peach
输出:
appleach
bananas
pearch

#include
#include
#include
#include
#include
#include
#include
 
using namespace std;
 
char a[1001], b[1001], s[10000];
int dp[1001][1001], len1, len2, k = 0;
 
void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else if(dp[i-1][j] > dp[i][j-1])
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = dp[i][j-1];
        }
    }
}
 
void llcs()
{
    int i, j, z = 0, k = 0;
    i = len1, j = len2;
    while(dp[i][j])
    {
        if(a[i-1] == b[j-1])
        {
            s[k++]=a[--i];
            j--;
        }
        else if(dp[i][j-1] < dp[i-1][j])
            s[k++] = a[--i];
        else if(dp[i][j-1] >= dp[i-1][j])
            s[k++] = b[--j];
    }
    while(i != 0)
        s[k++] = a[--i];
    while(j!=0)
        s[k++] = b[--j];
    for(z=k-1; z>=0; z--)
        printf("%c", s[z]);
    printf("\n");
}
 
int main()
{
    while(~scanf(" %s",a))
    {
        scanf(" %s", b);
        memset(dp, 0, sizeof(dp));
        len1 = strlen(a);
        len2 = strlen(b);
        lcs(len1, len2);
        llcs();
    }
    return 0;
}

你可能感兴趣的:(字符串)