hiho一下 第六十週 題目1 : String Matching Content Length dp 最長公共子序列

題目1 : String Matching Content Length

時間限制:10000ms
單點時限:1000ms
內存限制:256MB

描述

We define the matching contents in the strings of strA and strB as common substrings of the two strings. There are two additional restrictions on the common substrings.

The first restriction here is that every common substring's length should not be less than 3.  For example:

strA: abcdefghijklmn
strB: ababceghjklmn

The matching contents in strA and strB are substrings ("abc", "jklmn"). Note that though "e" and "gh" are common substrings of strA and strB, they are not matching content because their lengths are less than 3.

The second restriction is that the start indexes of all common substrings should be monotone increasing. For example:

strA: aaabbbbccc
strB: aaacccbbbb

The matching contents in strA and strB are substrings ("aaa", "bbbb"). Note that though "ccc" is common substring of strA and strB and has length not less than 3, the start indexes of ("aaa", "bbbb", "ccc") in strB are (0, 6, 3), which is not monotone increasing.

輸入

Two lines. The first line is strA and the second line is strB. Both strA and strB are of length less than 2100.

輸出

The maximum length of matching contents (the sum of the lengths of the common substrings).

樣例輸入
abcdefghijklmn
ababceghjklmn
樣例輸出
8
題意,要求最大的公共子序列,要求共公子序列的每一個子串都要是大於3的

類似於經典的最長公共子序列的做法。

dp[i][j][3]第一個串s1前i位  第二個串s2前j位,最後一個子串長度爲0 1 2 3(>=3)的最長公共子序列。

狀態轉移

1,dp[i][j][0]轉移到dp[i-1][j][0],dp[i-1][j][3] dp[i][j-1][0] dp[i][j-1][3];也就是第s1[i] 與s2[j]不匹配,直接轉移到前一位就可以了。

2.s1[i] == s2[j]則,dp[i][j][k]可以轉移到dp[i][j][k-1];也就是同是加一位。

初始化的時候,要注意,dp[0][i],dp[i][0]都爲0;

由於有n * n個狀態,狀態轉移爲o(1)所有總的複雜度爲O(n *n);

其次,給些測試數據

abcdefghijklmn
ababceghjklmn


aaabbbbccc
aaacccbbbb


aaa
aaa
ab
ab
aaaab
baaaa


abcba
bcbaa


babad
babacabad


abcdef
abcxcdef

#define N 2205
#define M 100005
#define maxn 2000005
#define MOD 1000000000000000007
int n,m,dp[N][N][4];
char s1[N],s2[N];
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
     while(SS(s1)!=EOF)
    {
        SS(s2);
        n = strlen(s1);m = strlen(s2);
        FI(n) FJ(m) For(k,0,4) dp[i][j][k] = -1;
        FI(n+1)
            dp[0][i][0] = dp[i][0][0] =0;
        For(i,1,n+1){
            For(j,1,m+1){
                dp[i][j][0] = max(dp[i][j][0],dp[i][j-1][0]);
                dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][0]);
                dp[i][j][0] = max(dp[i][j][0],dp[i][j-1][3]);
                dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][3]);
                if(s1[i-1] == s2[j-1]){
                    For(k,1,4)
                        if(dp[i-1][j-1][k-1] >= 0)
                            dp[i][j][k] = max(dp[i][j][k],dp[i-1][j-1][k-1] + 1);
                    if(dp[i-1][j-1][3] >= 0)
                        dp[i][j][3] = max(dp[i][j][3],dp[i-1][j-1][3] + 1);
                }
            }
        }
        printf("%d\n",max(0,max(dp[n][m][0],dp[n][m][3])));
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

發佈了463 篇原創文章 · 獲贊 57 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章