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
Sample Output
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; }
相关文章推荐
- cqoi2007 sum有关于x mod y求和的故事
- Openssl创建私有CA
- Android如何实现毛玻璃效果之Android高级模糊技术
- HDU 5281 Senior's Gun
- 字符串替换
- 数据结构-线性表
- 浅谈HTML中的块级元素和内联元素
- 怎么搭建php环境Apache+PHP+mysql
- BeautifulSoup解析网页信息
- 小球落下问题
- 黑马程序员————Java基础日常笔记---网络编程
- 推荐几个非常有用的开发工具之Android Studio插件
- 利用指针求最值
- Mac下Android studio 之NDK配置教程
- 组队选拔赛01 ---- trener
- Android 开发有哪些新技术出现?
- 协同函数的脚本编写
- HDU 2639 Bone Collector II(01背包 求第k大)
- Java基础学习之内部类
- python标准库:collections和heapq模块