KMP的next数组,对于next[i],是:1~i-1的最长的匹配的前缀和后缀的长度(也即在i位置匹配失败后,应该跳到的模式串的位置)

然后我们将所有满足要求的字串按照它的末尾位置分类。

  1. #include <cstdio>
  2. #include <cctype>
  3. #define M 10007
  4. #define maxn 200010
  5.  
  6. int n;
  7. char aa[maxn];
  8. int f[maxn], dp[maxn];
  9.  
  10. void gn( int &v ) {
  11. char ch, opt;
  12. while(!isdigit(ch=getchar())) opt=ch;
  13. v=ch-'';
  14. while( isdigit(ch=getchar())) v=v*+ch-'';
  15. if( opt=='-' ) v=-v;
  16. }
  17. void gc( char &ch ) {
  18. while(!isalpha(ch=getchar()));
  19. }
  20. void getfail() {
  21. f[] = f[] = ;
  22. for( int i=,j; i<n; i++ ) {
  23. j = f[i];
  24. while( j && aa[i]!=aa[j] ) j=f[j];
  25. f[i+] = aa[i]==aa[j] ? j+ : ;
  26. }
  27. }
  28. int main() {
  29. int T;
  30. gn(T);
  31. while( T-- ) {
  32. gn(n);
  33. for( int i=; i<n; i++ )
  34. gc(aa[i]);
  35. getfail();
  36. int sum=;
  37. dp[] = dp[] = ;
  38. for( int i=; i<=n; i++ ) {
  39. dp[i] = dp[f[i]]+(f[i]!=);
  40. sum = (sum+dp[i])%M;
  41. }
  42. sum += n;
  43. sum %= M;
  44. printf( "%d\n", sum );
  45. }
  46. }

hdu 3336的更多相关文章

  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

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

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

  4. (KMP)Count the string -- hdu -- 3336

    http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...

  5. hdu 3336 kmp+next数组应用

    分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...

  6. HDU 3336 扩展kmp

    题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...

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

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

  8. hdu 3336【Count the string】(KMP)

    一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...

  9. hdu 3336 Count the string(思维可水过,KMP)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

  10. HDU 3336 (KMP next性质) Count the string

    直接上传送门好了,我觉得他分析得非常透彻. http://972169909-qq-com.iteye.com/blog/1114968 #include <cstdio> #includ ...

随机推荐

  1. CSS实现箭头效果

    有时候网页中使用箭头以增强效果,一般的做法是使用图片,今天我们使用CSSCSS来实现“箭头效果”,使用CSS我们必须兼容所有浏览器(IE6.7.8.9.10.+),Chrome,Firefox,Ope ...

  2. kendo method:destroy 解决有些在kendo.all.js 的js 库里报错问题

    首先,不得不承认,kendo UI 是个不错的东西,特别对于一个前端开发到行不足的程序猿来说.而在我们使用过程中貌似还是会遇到各种奇怪的问题.比如我们会经常用到对一些控件进行重赋值. destroy ...

  3. dlmalloc(一)【转】

    转自:http://blog.csdn.net/ycnian/article/details/12971863 我们写过很多C程序了,经常会分配内存.记得刚学C语言时老师说过,可以向两个地方申请内存: ...

  4. Linux内存高,触发oom-killer问题解决

    最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 1. 首先确认该系统的版本是32位 ? #uname -a Linux alarm 2.6.9-67.ELsmp #1 S ...

  5. Linux下如何打开img镜像文件

    有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...

  6. vuex实例详解

    vuex是一个专门为vue.js设计的集中式状态管理架构.状态?把它理解为在data中的属性需要共享给其他vue组件使用的部分. 简单的说就是data需要共用的属性 一.小demo 已经用Vue脚手架 ...

  7. 虚拟机NAT网络设置

    1. 虚拟机设置 2. 本地网络设置 3. 本地虚拟网卡设置 4. 安装虚拟机,设置网络为NAT方式即可访问外网.

  8. html清屏 meta http-equiv="refresh" content="3">

    <meta http-equiv="refresh" content="3"> 什么意思? <meta http-equiv="re ...

  9. mongo备份&恢复

    1.备份数据: $mongodump -h 127.0.0.1 -u traderaccount -p kasumi -d traderaccount -o "/traderaccount& ...

  10. Hadoop(六)MapReduce的入门与运行原理

    一 MapReduce入门 1.1 MapReduce定义 Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架: Mapreduce核心功能是将用 ...