考虑dp[i]代表前缀s[1...i]出现的次数,必定有dp[nxt[i]] += dp[i]

倒着推就是了

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. int T, n, nxt[200005], dp[200005], ans;
  6. const int mod=10007;
  7. char a[200005];
  8. void mknxt(){
  9. int k=0;
  10. for(int i=2; i<=n; i++){
  11. while(k && a[i]!=a[k+1]) k = nxt[k];
  12. if(a[i]==a[k+1]) nxt[i] = ++k;
  13. }
  14. }
  15. int main(){
  16. cin>>T;
  17. while(T--){
  18. ans = 0;
  19. scanf("%d", &n);
  20. scanf("%s", a+1);
  21. memset(nxt, 0, sizeof(nxt));
  22. for(int i=1; i<=n; i++) dp[i] = 1;
  23. mknxt();
  24. for(int i=n; i>=1; i--)
  25. dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % mod;
  26. for(int i=1; i<=n; i++)
  27. ans = (ans + dp[i]) % mod;
  28. printf("%d\n", ans);
  29. }
  30. return 0;
  31. }

hdu3366 Count the string的更多相关文章

  1. Count the string[HDU3336]

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

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

  3. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  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. Count the string -- HDOJ 3336

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

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

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

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

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

  8. (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 ...

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

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

随机推荐

  1. C#运算符、控制流

    1 运算符 1.1 一元运算符: -(负号).+(正号):可以省略 1.2 二元运算符:     优先级,*(乘)./(除).%(取余).+(加).-(减).=(赋值) 二元赋值运算符,=.+=.-= ...

  2. linux 下源码编译环境配置

    yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel \ fontconfig fo ...

  3. touch事件总结

    $("body").on("touchstart", function(e) { e.preventDefault(); startX = e.original ...

  4. 自己实现的简单的grid

    12年在第一家公司的时候,有过很长一段时间在前端的使用研究上.一开始的时候使用ExtJs4.0 MVC 来开发前端,觉得里面的风转的组件非常好用,Panel.window.tree等等,简化了对于前端 ...

  5. Windows下用cmd命令安装及卸载服务[转]

    第一种方法: 1. 开始 ->运行 ->cmd2. cd到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(Framework版本号按IIS配置) ...

  6. hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)

    题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...

  7. getline()读入一整行

    string line; getline(cin, line); cin不能读入空行,用getline可以读入空行.

  8. linux 命令——7 mv(转)

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  9. <已解决> Eclipse启动失败

    参考:http://stackoverflow.com/questions/15404964/starting-eclipse-results-in-failed-to-create-java-vir ...

  10. [转载]AngularJS入门教程02:AngularJS模板

    是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...