题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少。

此题要用KMP的next和DP来做。

next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置。

下标从0开始。

设j=next[i],那么

如果j==0,回溯到起点说明该字符不匹配。

其他情况,说明字符串S[0,...j-1]与字符串S[0,..i-1]的某个后缀(准确的说是S[i-j,i-1])相同,这样的话,S[0,..i-1]的后缀(S[i-j,i-1])一定包含字符串S[0,..i-1]的后缀能够匹配的前缀个数,除此之外,S[0,..i-1]的后缀还应该包括自身这种情况。

设dp[i]表示字符串S[0,..i-1]的后缀可以匹配的前缀个数。

这样dp[i]=dp[next[i]]+1

答案ans=sum{dp[i]}(1<=i<=n)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define ll long long
  6. #define MOD 10007
  7. #define MAXN 200005
  8. using namespace std;
  9. char str[MAXN];
  10. int next[MAXN];
  11. int dp[MAXN];
  12. int main()
  13. {
  14. int T;
  15. scanf("%d",&T);
  16. while(T--)
  17. {
  18. int n;
  19. scanf("%d",&n);
  20. scanf("%s",str);
  21. ;i<n;++i)
  22. {
  23. int j=next[i];
  24. while(j&&str[i]!=str[j]) j=next[j];
  25. next[i+]=(str[i]==str[j])?j+:;
  26. }
  27. ;
  28. ;i<=n;++i)
  29. {
  30. dp[i]=(dp[next[i]]+)%MOD;
  31. ans=(ans+dp[i])%MOD;
  32. }
  33. printf("%d\n",ans);
  34. }
  35. ;
  36. }

HDU 3336 - Count the string(KMP+递推)的更多相关文章

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

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

  3. HDU 3336 Count the string KMP

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

  4. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  5. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

  6. 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) ...

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

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

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

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

  9. hdu 3336 Count the string(next数组)

    题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...

  10. HDU 3336 Count the string

    题解:利用next数组来保存前缀位置,递推求解. #include <cstdio> #include <cstring> char pat[200005]; int next ...

随机推荐

  1. 获取本机 Android 默认sha1 秘钥

    获取本机 Android 默认sha1 秘钥: 以Windows操作系统为例,打开CMD,运行以下指令将得到所有默认秘钥. keytool -list -v -keystore C:\Users\pa ...

  2. HTML5自学笔记[ 9 ]HTML5实现元素的拖放

    要想在html5中实现元素的拖放,被拖放元素就必须设置属性draggable="true";被拖放元素被放置的地方是另外一个元素,该元素是目标元素:这两个元素在拖放过程中都会触发不 ...

  3. Scala学习之: Hello Word!

    scala 是一门纯粹的面向对象的语言 , 结合了python和java的优点. 它和java的集合度很高,是一个在JVM上运行的非java语言(PS 其实scala也是用java编写的) 安装sca ...

  4. 用Visual C#向access添加数据

    (1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...

  5. HTML第二部分表单及使用Photoshop快速制作网页

    一.表单 <form id="" name="" method="post/get" action="负责处理的服务端&qu ...

  6. (巨坑)改了tpl文件之后,前端效果没反应

    通常前端修改很小的部分代码,并不会立即显示出效果.比如,原来是 ,修改后是 虽然只是加多了一个函数,但是作用很大,这种情况下,可能是由于浏览器缓存的原因,修改后的代码没有被重新加载.这个时候,只需要在 ...

  7. 转载 浅谈C/C++中的static和extern关键字

    浅谈C/C++中的static和extern关键字 2011-04-21 16:57 海子 博客园 字号:T | T   static是C++中常用的修饰符,它被用来控制变量的存贮方式和可见性.ext ...

  8. 转:Nginx 配置 location 总结及 rewrite 规则写法

    转: http://www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名 ...

  9. 让你的WPF程序使用多线程——BackgroundWorker

    在wpf中可以使用许多方法执行异步操作.利用.NET的芳芳就是手动创建一个新的System.Threading.Thread对象,提供一步代码,并使用THread.Start()方法加载代码.这种方法 ...

  10. ie8解决F12问题

    工作中,突然电脑上的ie8按F12掉不出来了,一直显示最小化.于是在网上找了很多方法,按这种方法可以解决问题. 1.cmd+r,输入regedit,调出 注册表编辑器. 2.HKEY_CURRENT_ ...