Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6875    Accepted Submission(s): 3191

Problem Description
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.
 
Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 
Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 
Sample Input
1
4
abab
 
Sample Output
6
 
Author
foreverlin@HNU
 
Source
 
题意:求给定字符串含前缀的数量
dp[i] 表示子串[1~i]含有以string[i]为结尾的前缀的种类数
  1. /*
  2. ID: LinKArftc
  3. PROG: 3336.cpp
  4. LANG: C++
  5. */
  6.  
  7. #include <map>
  8. #include <set>
  9. #include <cmath>
  10. #include <stack>
  11. #include <queue>
  12. #include <vector>
  13. #include <cstdio>
  14. #include <string>
  15. #include <utility>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <iostream>
  19. #include <algorithm>
  20. using namespace std;
  21. #define eps 1e-8
  22. #define randin srand((unsigned int)time(NULL))
  23. #define input freopen("input.txt","r",stdin)
  24. #define debug(s) cout << "s = " << s << endl;
  25. #define outstars cout << "*************" << endl;
  26. const double PI = acos(-1.0);
  27. const double e = exp(1.0);
  28. const int inf = 0x3f3f3f3f;
  29. const int INF = 0x7fffffff;
  30. typedef long long ll;
  31.  
  32. const int maxn = ;
  33. const int MOD = ;
  34. int dp[maxn];
  35. char str[maxn];
  36. int Next[maxn];
  37.  
  38. void getNext(char *p) {
  39. int i = , j = -;
  40. int len = strlen(p);
  41. Next[] = -;
  42. while (i < len) {
  43. if (j == - || p[i] == p[j]) {
  44. i ++; j ++;
  45. Next[i] = j;
  46. } else j = Next[j];
  47. }
  48. }
  49.  
  50. int main() {
  51. int T, n;
  52. scanf("%d", &T);
  53. while (T --) {
  54. scanf("%d", &n);
  55. scanf("%s", str);
  56. getNext(str);
  57. dp[] = ;
  58. int ans = ;
  59. for (int i = ; i <= n; i ++) {
  60. dp[i] = (dp[Next[i]] + ) % MOD;
  61. ans = (dp[i] + ans) % MOD;
  62. }
  63. printf("%d\n",ans);
  64. }
  65. return ;
  66. }

HDU3336(KMP + dp)的更多相关文章

  1. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  2. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  3. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  4. HDU5763 another meaning -(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...

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

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

  6. HDU 5763 Another Meaning (kmp + dp)

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

  7. hdu3689(kmp+dp)

    题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. 解法:dp+kmp优化.ans[i][j]表示i长度,走到了s的j位置的概率,当然这是在i之前没有出现s ...

  8. 【HDU 3336】Count the string(KMP+DP)

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

  9. hdu 6068--Classic Quotation(kmp+DP)

    题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

随机推荐

  1. iframe底边多出4px或5px解决办法

    问题: 在处理iframe框架自适应时,并且已经去掉iframe的边框,但仍然出现底边多出4px或5px高度的情况.如图 <div id="content"> < ...

  2. linux学习总结-----web前端①

    <html> <head> <title></title> <meta charset='utf-8'/> ... </head> ...

  3. CSP201604-1:折点计数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  4. Django源码分析之权限系统_擒贼先擒王

    乍见 Django内置的权限系统已经很完善了,加上django-guardian提供的功能,基本上能满足大部分的权限需求.暂且不说django-guardian,我们先来看下Django内置的权限系统 ...

  5. [Binary Search] Leetcode 35, 74

    35. Search Insert Position Description Given a sorted array and a target value, return the index if ...

  6. css如何选择相同class下的第一个class元素和最后一个元素?

    如图,如果像选择类名为  class="exerciseInfo"  中的第一个和最后一个div,做法如下: 选择第一个类名: .exerciseInfo: nth-of-type ...

  7. 教你一步一步在linux中正确的安装Xcache加速php

    #第一步,下载Xcache wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz #第一步非常简单,如果你下载不 ...

  8. Spark程序

    Spark认识&环境搭建&运行第一个Spark程序 2017-07-09 17:17 by 牛仔裤的夏天, 181 阅读, 0 评论, 收藏, 编辑 摘要:Spark作为新一代大数据计 ...

  9. 【bzoj2659】[Beijing wc2012]算不出的算式 数论

    题目描述 求,其中p和q是奇质数. 输入 只有一行,两个奇质数,分别表示p,q. 输出 一个数,表示算式结果. 样例输入 5 样例输出 6 题解 数论 神TM数学结论题... 当$p\neq q$时, ...

  10. visio中相关设置-菜单视图

    1.获取或设置窗口中页面的当前显示大小(缩放系数) Window.Zoom Dim dZoom As Double dZoom = m_Visio.Window.Zoom'获取显示比例 m_Visio ...