Another Meaning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 477 Accepted Submission(s): 223
Problem Description
As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”.
Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.
Limits
T <= 30
|A| <= 100000
|B| <= |A|
Output
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
iehiehieh
Sample Output
Case #1: 3
Case #2: 2
Case #3: 5
Case #4: 1
Hint
In the first case, “ hehehe” can have 3 meaings: “he”, “he”, “hehehe”.
In the third case, “hehehehe” can have 5 meaings: “hehe”, “he*he”, “hehe”, “**”, “hehehehe”.
不得不说当时做题时看到各高校A了第一题后以为是KMP+数学结果直到几小时之后才意识到可能不是数学思维,我们才换的DP的思路。做题量还是少啊..
主要思路就是用KMP求出 B串在A串中出现的末尾位置(可重叠)之后以这个为.为状态转移的思考点,
#include <iostream>
#include <algorithm>
#define mod 1000000007
#include <stdio.h>
#include <string.h>
#define MME(i,j) memset(i,j,sizeof(i))
#define maxs 1000010
using namespace std;
int nexts[maxs];
int lens,lenp;
char A[maxs],B[maxs];
void get_nexts(char *s)
{
int i=0,k=-1,len=strlen(s);
nexts[0]=-1;
while(i<len)
{
if(k==-1||s[i]==s[k])
{
nexts[++i]=++k;
}
else
k=nexts[k];
}
}
bool pos[maxs];
int kmp(char *s,char *p)
{
int i=0,j=0,ans=0;
lens=strlen(s),lenp=strlen(p);
while(i<lens&&j<lenp)
{
if(j==-1||s[i]==p[j])
{
i++;
j++;
}
else j=nexts[j];
if(j==lenp)
{
ans++;
pos[i]=1;
//ans%=mod;
j=nexts[j];
}
}
return ans;
}
int abs(int x)
{
if(x<0)
return -x;
return x;
}
int dp[maxs];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",A);
scanf("%s",B);
MME(pos,false);
MME(dp,0);
get_nexts(B);
int ans=kmp(A,B);
static int times=1;
// printf("ANs is %d\n",ans);
printf("Case #%d: ",times++);
dp[0]=1;
for(int i=1;i<=lens;i++)//lenp位 B串长度 lens 为 A串长度
{
dp[i]=dp[i-1];
if(pos[i]&&i>=lenp)
dp[i]=(dp[i-lenp]+dp[i])%mod;
}
printf("%d\n",dp[lens]%mod);
}
return 0;
}