题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5763

Another Meaning

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> 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 |A| |B|

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

题解

dp

用kmp处理出匹配成功的结尾的字母位置,然后就是考虑每个位置选和不选的两种情况,转移下就可以了。

代码

#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
#define bug(a) cout<<#a<<" = "<<a<<endl using namespace std; typedef __int64 LL; const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const int mod=1e9+7; char s1[maxn],s2[maxn];
int vis[maxn];
vector<int> pos;
LL dp[maxn]; int f[maxn];
void getFail(char *P){
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 n=strlen(T),m=strlen(P);
getFail(P);
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){
pos.push_back(i+1);
vis[i+1]=1;
}
}
} void init(){
pos.clear();
memset(vis,0,sizeof(vis));
} int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
init();
scanf("%s%s",s1,s2);
find(s1,s2);
memset(dp,0,sizeof(dp));
dp[0]=1;
int n=strlen(s1),m=strlen(s2);
for(int i=1;i<=n;i++){
//第i位不选
dp[i]=dp[i-1];
//第i位选
if(vis[i]) dp[i]+=dp[i-m];
dp[i]%=mod;
}
printf("Case #%d: %I64d\n",++kase,dp[n]);
}
return 0;
}

HDU 5763 Another Meaning KMP+DP的更多相关文章

  1. HDU 5763 Another Meaning(DP+KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...

  2. HDU 5763 Another Meaning (KMP/哈希+DP)

    题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...

  3. HDU 5763 Another Meaning

    HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. ...

  4. HDU 5763 Another Meaning dp+字符串hash || DP+KMP

    题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he** ...

  5. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  6. HDU 5763 Another Meaning (kmp + dp)

    Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...

  7. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  8. TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp

    Another Meaning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

随机推荐

  1. wpf仿QQ之窗体翻转

    很久以前在网上找过窗体翻转的Demo,但看得不是很明白,大多是内容的翻转,研究了下,现在分享给大家. 利用UIElement.RenderTransform 属性就能实现元素的呈现位置的转换,因此只需 ...

  2. C语言中的关键字

    1.C语言中的关键字都有实际的意义. 2.C语言中的23个关键字如下: char:声明字符型变量. short:声明短整型变量. int:声明整型变量. long:声明长整型变量. float:声明浮 ...

  3. 登陆peoplesoft的时候显示信息

    Signon Event Message Select selectPeopleTools, then selectUtilities, then selectAdministration, then ...

  4. Check for Data Duplicates on a Grid

    Here is a piece of code to prevent duplicate data on a specific field on a page grid. You can of cou ...

  5. jquery中each()函数

    今天在使用each函数时,发现写的js代码明明木有问题,为什么点击没有执行呢.js始终处于入门阶段,只好瞎鼓捣. 弄了半天,总算可以了.代码如下: <script type="text ...

  6. reader,字符流

    1. public class Demo1 { public static void main(String[] args) throws IOException { File file = new ...

  7. try-catch-finally中return语句的执行

    catch里return后还会执行finally吗??在java里,是的.但是值得注意的是,在存在try-catch-finally的方法中,return可能出现的位置有4个,在try中,在catch ...

  8. iOS开发的22个奇谲巧技

    结合自身的实践开发经验总结出了22个iOS开发的小技巧,以非常欢乐的语调轻松解决开发过程中所遇到的各种苦逼难题,光读着便已忍俊不禁. 1. TableView不显示没内容的Cell怎么办? 类似于图1 ...

  9. ifstat-网络接口监测工具

    ifstat-网络接口监测工具 http://gael.roualland.free.fr/ifstat/ ifstat is a tool to report network interfaces ...

  10. 004--VS C++ 绘制封闭图形

    //全局变量HPEN hPen;HBRUSH hBru[4];int sBru[4] = { HS_VERTICAL, HS_HORIZONTAL, HS_CROSS, HS_DIAGCROSS }; ...