hdu5763 Another Meaning (DP+KMP)

思路:

令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
   
   
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”.
 

Author
FZU
 

Source
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值