题目链接:

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处理出匹配成功的结尾的字母位置,然后就是考虑每个位置选和不选的两种情况,转移下就可以了。

代码

  1. #include<map>
  2. #include<queue>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<string>
  7. #include<iostream>
  8. #include<algorithm>
  9. #define X first
  10. #define Y second
  11. #define mkp make_pair
  12. #define lson (o<<1)
  13. #define rson ((o<<1)|1)
  14. #define M (l+(r-l)/2)
  15. #define bug(a) cout<<#a<<" = "<<a<<endl
  16. using namespace std;
  17. typedef __int64 LL;
  18. const int maxn=1e5+10;
  19. const int INF=0x3f3f3f3f;
  20. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  21. const int mod=1e9+7;
  22. char s1[maxn],s2[maxn];
  23. int vis[maxn];
  24. vector<int> pos;
  25. LL dp[maxn];
  26. int f[maxn];
  27. void getFail(char *P){
  28. int m=strlen(P);
  29. f[0]=0; f[1]=0;
  30. for(int i=1;i<m;i++){
  31. int j=f[i];
  32. while(j&&P[i]!=P[j]) j=f[j];
  33. f[i+1]=P[i]==P[j]?j+1:0;
  34. }
  35. }
  36. void find(char* T,char *P){
  37. int n=strlen(T),m=strlen(P);
  38. getFail(P);
  39. int j=0;
  40. for(int i=0;i<n;i++){
  41. while(j&&P[j]!=T[i]) j=f[j];
  42. if(P[j]==T[i]) j++;
  43. if(j==m){
  44. pos.push_back(i+1);
  45. vis[i+1]=1;
  46. }
  47. }
  48. }
  49. void init(){
  50. pos.clear();
  51. memset(vis,0,sizeof(vis));
  52. }
  53. int main() {
  54. int tc,kase=0;
  55. scanf("%d",&tc);
  56. while(tc--){
  57. init();
  58. scanf("%s%s",s1,s2);
  59. find(s1,s2);
  60. memset(dp,0,sizeof(dp));
  61. dp[0]=1;
  62. int n=strlen(s1),m=strlen(s2);
  63. for(int i=1;i<=n;i++){
  64. //第i位不选
  65. dp[i]=dp[i-1];
  66. //第i位选
  67. if(vis[i]) dp[i]+=dp[i-m];
  68. dp[i]%=mod;
  69. }
  70. printf("Case #%d: %I64d\n",++kase,dp[n]);
  71. }
  72. return 0;
  73. }

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. css style与class之间的区别,cssclass

    问题描述:    网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...

  2. MyEclipse 选中属性或方法后 相同的不变色了?

    MyEclipse 选中属性或方法后 相同的不变色了? myeclipse-->windows-->java-->Editor-->Mark Occurrences 把所有的框 ...

  3. ASP.NET中Server对象的几个方法

    HtmlDecode 已重载. 对已被编码以消除无效 HTML 字符的字符串进行解码.HtmlEncode 已重载. 对要在浏览器中显示的字符串进行编码.MapPath 返回与 Web 服务器上的指定 ...

  4. [leetcode]_K Sum 问题

    问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...

  5. php static延迟静态绑定

    如果你是一个懒惰的程序员,你看到以下代码可能会恼火 abstract class U{ } class u1 extends U{ public static function create(){ r ...

  6. mysql存储引擎(mysql学习六)

    存储引擎 现在只有InnoDB支持外键 上接着学习笔记五 class表中有外键,所以不能修改存储引擎 表类型   默认的服务器表类型,通过my.ini可以配置    Default-storage-e ...

  7. js闭包理解实例小结

    Js闭包 闭包前要了解的知识  1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...

  8. 删:[CentOS 7] 安装nginx

    下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...

  9. MySQL线上执行大事务或锁表操作

    前提 在线执行一些大事务或锁表操作(给某个核心级表加一列或者执行修改操作),此时不但主库从库要长时间锁表,主从延迟也会变大.未避免大事务sql对整个集群产生影响,,我们希望一条SQL语句只在Maste ...

  10. mac ulimit

    sudo sysctl -w kern.maxfilesperproc=1048576ulimit -n 1048576