链接:

https://vjudge.net/problem/HDU-3336

题意:

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:

s: "abab"

The prefixes are: "a", "ab", "aba", "abab"

For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.

The answer may be very large, so output the answer mod 10007.

思路:

计算s前缀出现的次数, 考虑扩展KMP的Nex数组, 从i位置开始从s0开始匹配的长度.

即这些长度的前缀都出现过.累加和即可.

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. //#include <memory.h>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <algorithm>
  10. #include <math.h>
  11. #include <stack>
  12. #include <string>
  13. #include <assert.h>
  14. #include <iomanip>
  15. #include <iostream>
  16. #include <sstream>
  17. #define MINF 0x3f3f3f3f
  18. using namespace std;
  19. typedef long long LL;
  20. const int MAXN = 2e5+10;
  21. const int MOD = 1e4+7;
  22. char s1[MAXN], s2[MAXN];
  23. int Next[MAXN], Exten[MAXN];
  24. void GetNext(char *s)
  25. {
  26. int len = strlen(s);
  27. int a = 0, p = 0;
  28. Next[0] = len;
  29. for (int i = 1;i < len;i++)
  30. {
  31. if (i >= p || i+Next[i-a] >= p)
  32. {
  33. if (i >= p)
  34. p = i;
  35. while (p < len && s[p] == s[p-i])
  36. p++;
  37. Next[i] = p-i;
  38. a = i;
  39. }
  40. else
  41. Next[i] = Next[i-a];
  42. }
  43. }
  44. void ExKmp(char *s, char *t)
  45. {
  46. int len = strlen(s);
  47. int a = 0, p = 0;
  48. GetNext(t);
  49. for (int i = 0;i < len;i++)
  50. {
  51. if (i >= p || i + Next[i-a] >= p)
  52. {
  53. if (i >= p)
  54. p = i;
  55. while (p < len && s[p] == t[p-i])
  56. p++;
  57. Exten[i] = p-i;
  58. a = i;
  59. }
  60. else
  61. Exten[i] = Next[i-a];
  62. }
  63. }
  64. int main()
  65. {
  66. int t, n;
  67. scanf("%d", &t);
  68. while (t--)
  69. {
  70. scanf("%d", &n);
  71. scanf("%s", s1);
  72. GetNext(s1);
  73. int res = 0;
  74. for (int i = 0;i < strlen(s1);i++)
  75. res = (res+Next[i])%MOD;
  76. printf("%d\n", res);
  77. }
  78. return 0;
  79. }

HDU-3336-Count the string(扩展KMP)的更多相关文章

  1. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  3. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  4. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  5. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. Redis(1.9)Redis主从复制

    [1]实验环境 CentOS7.5 + Redis4.0.11 架构:原生1主2从,做实验机器有限,从库双实例 主库:192.168.135.170 从库1:192.168.135.171~6379 ...

  2. SQLite进阶-11.Join

    目录 JOIN 交叉连接 - CROSS JOIN 内连接 - INNER JOIN 外连接 - OUTER JOIN JOIN JOIN 子句用于结合两个或者多个数据表的数据,基于这些表之间的共同字 ...

  3. VUE前后台分离

    VUE前后台分离 配置js环境 jQuery >: cnpm install jquery vue/cli 3 配置jQuery:在vue.config.js中配置(没有,手动项目根目录下新建) ...

  4. MySQL之主键

    一.主键  primary key (唯一标识 .不能重复.不能为空) 1.主键-----是表中的字段,这个字段能唯一标识一条记录.例如 学生表(学号.姓名,年级)里的学号,不能重复.不能为空: 课程 ...

  5. Win32汇编-编写PE结构解析工具

    汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...

  6. Codeforces1263D-Secret Passwords

    题意 给n个字符串,两个字符串之间如果有相同的字符,那么两个就等价,等价关系可以传递,问最后有多少个等价类. 分析 考虑并查集或者dfs联通块,如果是并查集的话,对于当前字符串的某个字符,肯定要和这个 ...

  7. 和 Python 2.x 说再见!项目移到python3

    如果你仍在使用 2.x,那么是时候将你的代码移植到 Python 3 了. 在技术的长河中,软件.工具.系统等版本的迭代本是常事,但由于使用习惯.版本的兼容性.易用性等因素,很多用户及开发者在使用或做 ...

  8. (二)创建基于maven的javaFX项目

    首先使用IDEA创建一个javaFX项目 点击finish,这就创建完成了JavaFX项目,只有将其转换为maven项目即可,如图:

  9. vue 项目中如何在页面刷新的状态下保留数据

    1.问题:在vue项目中,刷新页面之后,我当前打开的所有菜单,都消失,我如何实现刷新之后页面仍然是刷新之前的状态 效果图: 解决方法: 使用vuex作状态管理: 将vuex里面的数据同步更新到loca ...

  10. TVM设备添加以及代码生成

    因为要添加的设备是一种类似于GPU的加速卡,TVM中提供了对GPU编译器的各种支持,有openCl,OpenGL和CUDA等,这里我们选取比较熟悉的CUDA进行模仿生成.从总体上来看,TVM是一个多层 ...