Another Meaning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 917    Accepted Submission(s): 434

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

题意:给你一个主串一个子串,然后主串中匹配到子串就可以把匹配部分改为*(也可以不改),问主串有多少钟不同的样子;

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <vector>
  6. #include <queue>
  7. #include <cstring>
  8. #include <string>
  9. #include <algorithm>
  10. using namespace std;
  11. typedef long long ll;
  12. typedef unsigned long long ull;
  13. #define MM(a,b) memset(a,b,sizeof(a));
  14. #define inf 0x7f7f7f7f
  15. #define FOR(i,n) for(int i=1;i<=n;i++)
  16. #define CT continue;
  17. #define PF printf
  18. #define SC scanf
  19. const int mod=1000000007;
  20. const int N=1e5+10;
  21.  
  22. int la,lb;
  23. ll dp[N];
  24. char a[N],b[N];
  25. ull ha[N],hb;
  26. ull seed=13331;
  27.  
  28. void init(){
  29. hb=0;
  30. for(int i=0;i<lb;i++)
  31. hb=hb*seed+b[i];
  32. ull base=1;
  33. for(int i=1;i<=lb-1;i++) base*=seed;
  34. ha[0]=a[0];
  35. for(int i=1;i<=lb-1;i++)
  36. ha[i]=ha[i-1]*seed+a[i];
  37. for(int i=lb;i<la;i++)
  38. ha[i]=(ha[i-1]-a[i-1-(lb-1)]*base)*seed+a[i];
  39. }
  40.  
  41. int main()
  42. {
  43. int cas,kk=0;
  44. scanf("%d",&cas);
  45. while(cas--){
  46. scanf("%s",a);
  47. scanf("%s",b);
  48. la=strlen(a);lb=strlen(b);
  49. if(la<lb) {printf("Case #%d: 1\n",++kk);CT;}
  50. init();
  51. for(int i=0;i<=lb-1;i++) dp[i]=1;
  52. if(ha[lb-1]==hb) dp[lb-1]=2;
  53. for(int i=lb;i<la;i++){
  54. dp[i]=dp[i-1]%mod;
  55. if(ha[i]==hb) dp[i]=(dp[i]+dp[i-lb])%mod;
  56. }
  57. printf("Case #%d: %lld\n",++kk,dp[la-1]%mod);
  58. }
  59. return 0;
  60. }

  分析:错误点:

1.BKDRhash不太熟练,只会原来的最后输出&的形式,导致最后计算复杂;

改进:BKDRhash的形式:pre*seed+a[i],seed为13331之类的大素数,pre为i以前的哈希值;

a[i]就是字符

2,没有想到dp,当前下标i的话,dp[i]的数值,如果当前i并未匹配到子串,dp[i]=dp[i-1];

如果匹配到子串,dp[i]=dp[i-1]+dp[i-lb];

TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp的更多相关文章

  1. HDU 5763 Another Meaning (kmp + dp)

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

  2. HDU 5763 Another Meaning

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

  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)

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

  5. HDU 5763 Another Meaning(DP+KMP)

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

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

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

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

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

  8. HDU 5763 Another Meaning(FFT)

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

  9. HDU 5763:Another Meaning(字符串匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Problem Description   As is known to ...

随机推荐

  1. table表格整体居中 和 table表格中各行各列内容居中

    1.table表格整个居中<div style="text-align: center;"> <table border="1" style= ...

  2. win10 Snipaste 截图软件

    安装教程:搜索 snipaste,网上可以直接下载 使用教程: 1)截图按钮:F1 2)粘贴按钮:F3

  3. 【ES6 】var/let/const的区别

    var 声明变量 没有区级作用域 可以预解析 可以重复定义 声明的全局变量属于顶层对象(window)的属性 let 声明变量 有块级作用域 没有预解析 不可以重复定义 声明的全局变量不属于顶层对象( ...

  4. 最全MySQL面试题和答案

    Mysql 的存储引擎,myisam和innodb的区别. 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁,适合小数据,小并发. 2.innodb是支持事务的存 ...

  5. 记录FTPClient超时处理的相关问题(转)

    https://www.cnblogs.com/dasusu/p/10006899.html 记录 FTPClient 超时处理的相关问题   apache 有个开源库:commons-net,这个开 ...

  6. [CSS] w3c 盒模型 和 IE 盒模型

  7. JS-逻辑运算符的与,或,非

    JS-逻辑运算符的与,或,非 1.非 所谓非,就是取反,非真即假,非假即真. 非运算符不仅仅只能用于布尔值,其他数据类型也是可以的,如下: 1.如果操作数是一个对象,返回false 2.如果操作数是一 ...

  8. firefox(火狐中的兼容问题总结)

    1.firefox 下 默认情况 <input   type="number"> 只允许整数其他的都会报错,红色提示: 这时候可以添加参数 step="0.0 ...

  9. php判断

    <?php $str = '我是张三?'; preg_match("/张三/", $str, $match); if($match) { echo ' 张三在文本中'; } ...

  10. 并发编程J.U.C之AQS剖析

    一.j.u.c简介 在说主题AQS之前,我们有必要先来说一下J.U.C 顾名思义J.U.C就是java.util.concurrent,java并发工具包.由我们的并发大师老爷子Doug Lea亲自操 ...