题目链接:这里
题意:
给你一个字符串A,给你一个字符串B。
说B这个字符串有两个意思,请问字符串A一共有多少个意思
解法:
DP,dp[i]表示以i结尾有多少种意思,然后用kmp去转移就好了。首先dp[i] = dp[i-1] 当i点是匹配点是, dp[i] += dp[i-len2]。
//HDU 5763
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5 + 15;
int fail[maxn], match[maxn], dp[maxn], len1, len2;
char a[maxn], b[maxn];
inline void add(int &x, int y){
x += y;
if(x >= mod) x -= mod;
}
void build(){
memset(fail, 0, sizeof(fail));
fail[0] = fail[1] = 0;
for(int i = 1; i < len2; i++){
int j = fail[i];
while(j && b[i] != b[j]) j = fail[j];
fail[i + 1] = b[i] == b[j] ? j + 1 : 0;
}
}
int main()
{
int T, ks = 0;
scanf("%d", &T);
while(T--){
scanf("%s%s", a, b);
len1 = strlen(a), len2 = strlen(b);
memset(match, 0, sizeof(match));
memset(dp, 0, sizeof(dp));
build();
int j = 0;
for(int i = 0; i < len1; i++){
while(j && a[i] != b[j]) j = fail[j];
if(a[i] == b[j]) ++j;
if(j == len2) match[i - len2 + 1] = 1, j = fail[j];
}
dp[0] = 1;
for(int i = 0; i < len1; i++){
add(dp[i+1], dp[i]);
if(match[i]) add(dp[i+len2], dp[i]);
}
printf("Case #%d: %d\n", ++ks, dp[len1]);
}
return 0;
}