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

Another Meaning

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

 
题意:每个例子的第二个串有两个意思,然后求第一个串可以表达多少种意思。
思路:做的时候DP方程推不出来,太渣了。dp[i]表示以i结尾长度的串有多少种意思,如果不替换的话,dp[i] = dp[i-1],如果当前匹配的话可以替换,要加上前面的dp[i-m],赛后补题的时候发现string类有个substr的函数很厉害。
 
 #include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 100005
#define MOD 1000000007
/*
对于这个问题,显然可以进行DP:
令dp[i]表示到i结尾的字符串可以表示的不同含义数,那么考虑两种转移: 末尾不替换含义:dp[i - 1] 末尾替换含义:dp[i - |B|] (A.substr(i - |B| + 1,|B|) = B) 那么对于末尾替换含义的转移,需要快速判断BB能不能和当前位置的后缀匹配,kmp或者hash判断即可。 复杂度:O(N)
*/
long long dp[N];
string s, str; int main()
{
int t;
cin >> t;
for(int cas = ; cas <= t; cas++) {
memset(dp, , sizeof(dp));
cin >> s >> str;
int n = s.size();
int m = str.size();
for(int i = ; i < m; i++)
dp[i] = ;
for(int i = m; i <= n; i++) {
dp[i] = dp[i-];
string ss = s.substr(i - m, m);
if(ss == str)
dp[i] += dp[i-m] % MOD;
dp[i] %= MOD;
}
printf("Case #%d: ", cas);
cout << dp[n] << endl;
}
return ;
}

HDU 5763:Another Meaning(字符串匹配)的更多相关文章

  1. HDU 5763 Another Meaning

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

  2. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  3. HDU 5763 Another Meaning KMP+DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...

  4. HDU 5763 Another Meaning (kmp + dp)

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

  5. HDU 5763 Another Meaning(DP+KMP)

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

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

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

  7. HDU 5763 Another Meaning(FFT)

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

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

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

  9. TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp

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

  10. HDU 1711 Number Sequence(字符串匹配)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. js 动态生成button 并设置click事件

    <div id="MyDiv"></div> <script> function AddButton() { var MyDiv =docume ...

  2. ASP .NET My97DatePicker

    My97DatePicker http://jingyan.baidu.com/article/e6c8503c7244bae54f1a18c7.html <input type="t ...

  3. C# IDisposable接口的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. SQLServer 事务复制中使用脚本添加某个对象的发布

    原文:SQLServer 事务复制中使用脚本添加某个对象的发布 -- use [发布库] --添加表:创建项目并将其添加到发布中 exec sp_addarticle @publication = N ...

  5. Android零基础入门第48节:可折叠列表ExpandableListView

    原文:Android零基础入门第48节:可折叠列表ExpandableListView 上一期学习了AutoCompleteTextView和MultiAutoCompleteTextView,你已经 ...

  6. 使用Advanced Installer 13.1打包发布 Windows Service服务程序

    原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...

  7. 比快更快——微软LightGBM

    LightGBM介绍 xgboost是一种优秀的boosting框架,但是在使用过程中,其训练耗时过长,内存占用比较大.微软在2016年推出了另外一种boosting框架--lightgbm,在不降低 ...

  8. iOS密码框实现(二)取消确定按钮

    由于将确定按钮去掉了,所以需要重新修改下代码,当输入第四个数字时,自动进入房间.   iOS 密码框效果图:     实现方式:   首先声明一个block初始化方法,因为这只是个框框,并不需要处理网 ...

  9. OpenCv的python环境搭建

    1.python的安装参看 http://www.cnblogs.com/samo/p/6734403.html 2.OpenCv安装.opencv2.4.10可以支持vc10/vc11/vc12,o ...

  10. Nginx反向代理负载均衡的容器化部署

    首先,在home目录创建microservices目录,开启第一篇章. cd ~ && mkdir microservices && cd microservices ...