hdu5763 Another Meaning(DP+KMP)

来源:互联网 发布:高铁 刘志军 知乎 编辑:程序博客网 时间:2024/05/17 21:38
思路:

令dp[i]表示到i结尾的字符串可以表示的不同含义数,那么考虑两种转移:

末尾不替换含义:dp[i - 1]

末尾替换含义:dp[i - |B|]  (A.substr(i - |B| + 1,|B|) = B)

那么对于末尾替换含义的转移,需要快速判断BB能不能和当前位置的后缀匹配,kmp或者hash判断即可。

复杂度:O(N)


#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N=2*1e5+10;const int INF=0x3f3f3f3f;const int mod=1000000007;int cas=1,T;int fail[N],v[N];LL dp[N];char s[N],p[N];void getFail(char *P,int *f){    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 *f){    int n=strlen(T),m=strlen(P);    getFail(P,f);    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) v[i-m+1]=1;    }}int main(){    //freopen("1.in","w",stdout);    //freopen("1.in","r",stdin);    //freopen("1.out","w",stdout);    scanf("%d",&T);    while(T--)    {        memset(v,0,sizeof(v));        memset(dp,0,sizeof(dp));        s[0]=0;p[0]=0;        scanf("%s%s",s,p);        int len=strlen(p);        int n=strlen(s);        find(s,p,fail);        //for(int i=0;i<=n;i++) printf("%d",v[i]);printf("next\n");        for(int i=0;i<=n;i++) dp[i]=1;        for(int i=0;i<=n;i++)        {            if(i) dp[i]=(dp[i-1]+dp[i]-1)%mod;            if(v[i])             {                dp[i+len]=(dp[len+i]+dp[i])%mod;                //dp[i]=1;            }            //for(int i=0;i<=n;i++) printf("%d",dp[i]);printf("\n");        }        printf("Case #%d: %lld\n",cas++,dp[n]);    }    //printf("time=%.3lf\n",(double)clock()/CLOCKS_PER_SEC);    return 0;}


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
4hehehehehewoquxizaolehehewoquxizaoleheheheheheheowoadiuhzgneninouguriehiehieh
 

Sample Output
Case #1: 3Case #2: 2Case #3: 5Case #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”.
 

Author
FZU
 

Source
2016 Multi-University Training Contest 4
 


0 0
原创粉丝点击