CodeForces 163A
来源:互联网 发布:学知不足 业精于勤 编辑:程序博客网 时间:2024/05/22 15:20
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string sor different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ cor b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.
Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
aaaa
5
codeforcesforceofcode
60
Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1]t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".
题目大意:两个字符串,求第一个的所有子串(连续)与第二个的所有子序列(不一定连续)相同的情况之和;如果用最容易想到的办法:先枚举一个串的每一个子串,用枚举的这个子串与另一字符串的子串匹配,依次进行下去,肯定会超时。
#include<bits/stdc++.h>using namespace std;#define N 5005#define mod 1000000007typedef long long ll;char a[N],b[N];int lena,lenb;ll dp[N][N];int main(){ int i,j; while(~scanf("%s%s",b+1,a+1)) { lena=strlen(a+1); lenb=strlen(b+1); ll ans=0; for(i=1;i<=lena;i++) { for(j=1;j<=lenb;j++) { dp[i][j]=dp[i-1][j]; if(a[i]==b[j]) { // cout<<dp[i][j]<<"---"<<dp[i-1][j-1]+1<<" "<<i<<"--"<<j<<endl; dp[i][j]=(dp[i][j]+dp[i-1][j-1]+1)%mod; ans=(ans+dp[i-1][j-1]+1)%mod; } } } printf("%I64d\n",ans); } return 0;}
- CodeForces 163A
- Codeforces 164B || Codeforces 163A
- CodeForces-a
- Codeforces 903A A
- Codeforces Round #163 (Div. 2) A题
- codeforces 163A A. Substring and Subsequence(dp)
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- A. Divisibility CodeForces 597A
- codeforces 417A A. Elimination
- CodeForces 534A A. Exam
- codeforces#98 a
- codeforces#99 a
- codeforces 105 div2 A
- codeforces #121 A
- vue-router使用 history 模式刷新后404问题
- Android中JS与Native原生交互
- 消息队列常见问题
- laravel 某些用法
- win7便笺和截图无法使用解决办法
- CodeForces 163A
- jdbc batch批量处理与非批量的比较
- 计算排列组合程序片段
- vue2.0使用swiper组件实现轮播
- 设计一个算法,计算出n阶乘中尾部零的个数
- 24. Swap Nodes in Pairs
- MVP模式
- excel入门,如何玩转excel,你早该这么玩Excel笔记10
- iOS常用第三方库大全