题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5763
Another Meaning
Memory Limit: 65536/65536 K (Java/Others)
For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.
样例
sample input
4
hehehe
hehe
woquxizaolehehe
woquxizaole
hehehehe
hehe
owoadiuhzgneninougur
iehiehiehsample output
Case #1: 3
Case #2: 2
Case #3: 5
Case #4: 1
题解
dp
用kmp处理出匹配成功的结尾的字母位置,然后就是考虑每个位置选和不选的两种情况,转移下就可以了。
代码
#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
#define bug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef __int64 LL;
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const int mod=1e9+7;
char s1[maxn],s2[maxn];
int vis[maxn];
vector<int> pos;
LL dp[maxn];
int f[maxn];
void getFail(char *P){
int m=strlen(P);
f[0]=0; f[1]=0;
for(int i=1;i<m;i++){
int j=f[i];
while(j&&P[i]!=P[j]) j=f[j];
f[i+1]=P[i]==P[j]?j+1:0;
}
}
void find(char* T,char *P){
int n=strlen(T),m=strlen(P);
getFail(P);
int j=0;
for(int i=0;i<n;i++){
while(j&&P[j]!=T[i]) j=f[j];
if(P[j]==T[i]) j++;
if(j==m){
pos.push_back(i+1);
vis[i+1]=1;
}
}
}
void init(){
pos.clear();
memset(vis,0,sizeof(vis));
}
int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
init();
scanf("%s%s",s1,s2);
find(s1,s2);
memset(dp,0,sizeof(dp));
dp[0]=1;
int n=strlen(s1),m=strlen(s2);
for(int i=1;i<=n;i++){
//第i位不选
dp[i]=dp[i-1];
//第i位选
if(vis[i]) dp[i]+=dp[i-m];
dp[i]%=mod;
}
printf("Case #%d: %I64d
",++kase,dp[n]);
}
return 0;
}