您的位置:首页 > 其它

HDU 5282 Senior's String

2015-07-18 23:52 169 查看
Problem Description

Xuejiejie loves strings most. In order to win the favor of her, a young man has two strings X, Y to
Xuejiejie. Xuejiejie has never seen such beautiful strings! These days, she is very happy. But Xuejiejie is missish so much, in order to cover up her happiness, she asks the young man a question. In face of Xuejiejie, the young man is flustered. So he asks
you for help.
The question is that : Define the L as
the length of the longest common subsequence of X and Y.(
The subsequence does not need to be continuous in the string, and a string of length L has 2L subsequences
containing the empty string ). Now Xuejiejie comes up with all subsequences of length L of
string X,
she wants to know the number of subsequences which is also the subsequence of string Y.

Input

In the first line there is an integer T,
indicates the number of test cases.
In each case:
The first line contains string X,
a non-empty string consists of lowercase English letters.
The second line contains string Y,
a non-empty string consists of lowercase English letters.
1≤|X|,|Y|≤1000, |X| means
the length of X.

Output

For each test case, output one integer which means the number of subsequences of length L of X which
also is the subsequence of string Y modulo 109+7.

Sample Input

2
a
b
aa
ab


Sample Output

1
2
dp
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
const int maxn = 1005;
const __int64 base = 1000000007;
int n, m, T, na, nb;
char a[maxn], b[maxn];
__int64 f[maxn][maxn], sum[maxn][maxn], ans;

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s%s", a, b);
        na = strlen(a);
        nb = strlen(b);
        memset(f, 0, sizeof(f));
        memset(sum, 0, sizeof(sum));
        for (int i = 0; i <= na; i++) sum[i][0] = 1;
        for (int i = 0; i <= nb; i++) sum[0][i] = 1;
        for (int i = 1; i <= na; i++)
            for (int j = 1; j <= nb; j++)
            {
                if (a[i - 1] == b[j - 1])
                {
                    f[i][j] = f[i - 1][j - 1] + 1;
                    sum[i][j] = sum[i - 1][j - 1];
                    if (f[i][j] == f[i - 1][j]) sum[i][j] += sum[i - 1][j];
                    //if (f[i][j] == f[i][j - 1]) sum[i][j] += sum[i][j - 1];
                }
                else
                {
                    f[i][j] = max(f[i - 1][j], f[i][j - 1]);
                    if (f[i][j] == f[i - 1][j]) sum[i][j] += sum[i - 1][j];
                    if (f[i][j] == f[i][j - 1]) sum[i][j] += sum[i][j - 1];
                    if (f[i][j - 1] == f[i - 1][j] && f[i][j] == f[i - 1][j - 1])
                        sum[i][j] -= sum[i - 1][j - 1];
                }
                sum[i][j] %= base;
            }
        //printf("%I64d\n", f[na][nb]);
        (sum[na][nb] += base) %= base;
        printf("%I64d\n", sum[na][nb]);
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: