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)
那么对于末尾替换含义的转移,需要快速判断B能不能和当前位置的后缀匹配,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.
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|
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: 1HintIn 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
- hdu5763 Another Meaning(DP+KMP)
- HDU5763 another meaning -(KMP+DP)
- hdu5763 Another Meaning(kmp+dp)
- hdu5763 多校4 Another Meaning【kmp+dp】
- (多校第四场1001)HDU5763 Another Meaning (DP、KMP)
- HDU5763 Another Meaning
- hdu5775 Another Meaning(扩展kmp+dp)
- HDU-5763-Another Meaning(KMP+DP)
- HDU 5763(Another Meaning kmp+dp)
- HDU 5763 Another Meaning(kmp+dp)
- HDU Another Meaning DP+KMP
- HDU5763 Another Meaning动态规划
- hdu 5763 Another Meaning KMP+DP(多校)
- HDU 5763Another Meaning (DP+KMP||后缀数组)
- Hdu-5763 Another Meaning(DP+KMP)
- HDU 5763 Another Meaning(KMP+DP)
- HDOJ 5763 Another Meaning(kmp+dp)
- HDU 5763 Another Meaning dp+kmp优化
- 在linux下使用curl访问 多参数url GET参数问题
- Java集合简单案例
- 六个例子彻底理解finally语句块
- 阻塞队列简介
- 1分钟教你学会用adb进行无线调试
- hdu5763 Another Meaning(DP+KMP)
- Section1:UEFI概述
- 设A是n*n的对称矩阵,将A的对角线及对角线上方的元素以列为主的次序存放在一维数组B[1..n(n+1)/2]中,对上述任一元素aij(1<=i,j<=n,且i<=j)在B中的位置为()
- 复制文件夹下的内容到另一个文件夹下
- JS插件收藏
- poj 1952 BUY LOW, BUY LOWER【解法一】
- Linux学习--进程创建
- HDU2521:反素数 .
- POJ 1466 最大独立点集