地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336

题目:

Count the string

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

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
HDOJ Monthly Contest – 2010.03.06
 
Recommend
lcy
 思路:next数组+dp
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4
 5 using namespace std;
 6
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 const double eps=1e-8;
11 const int K=1e6+7;
12 const int mod=1e9+7;
13
14 int nt[K],ans[K],dp[K];
15 char sa[K],sb[K];
16 void kmp_next(char *T,int *next)
17 {
18     next[0]=0;
19     for(int i=1,j=0,len=strlen(T);i<len;i++)
20     {
21         while(j&&T[i]!=T[j]) j=next[j-1];
22         if(T[i]==T[j])  j++;
23         next[i]=j;
24     }
25 }
26 int kmp(char *S,char *T,int *next)
27 {
28     int ans=0;
29     int ls=strlen(S),lt=strlen(T);
30     kmp_next(T,next);
31     for(int i=0,j=0;i<ls;i++)
32     {
33         while(j&&S[i]!=T[j]) j=next[j-1];
34         if(S[i]==T[j])  j++;
35         if(j==lt)   ans++;
36     }
37     return ans;
38 }
39 int main(void)
40 {
41     int t,n;cin>>t;
42     while(t--)
43     {
44         int ans=0;
45         scanf("%d%s",&n,sa);
46         kmp_next(sa,nt);
47         dp[0]=1;
48         for(int i=1;i<n;i++)
49             dp[i]=dp[nt[i]-1]+1;
50         for(int i=0;i<n;i++)
51             ans=(ans+dp[i])%10007;
52         printf("%d\n",ans);
53     }
54     return 0;
55 }

转载于:https://www.cnblogs.com/weeping/p/6669793.html

hdu3336 Count the string相关推荐

  1. [HDU3336]Count the string(KMP+DP)

    Solution 不稳定的传送门 对KMP的灵活应用 设dp[i]表示前[1,i]的答案 那么dp[i]=dp[p[i]]+1,p[i]为失配函数 Code #include <cstdio&g ...

  2. HDU 3336 Count the string(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,计算其所有前缀在该字符串出现的次数的总和. 思路:next[j]=i,代表 ...

  3. HDU 3336 Count the string KMP

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

  4. 牛客多校4 - Count New String(序列自动机+广义后缀自动机)

    题目链接:点击查看 题目大意: 题目分析:首先观察到集集合 A 中那个套娃的表示,外层的范围是 [ x1 , y1 ] ,内层是 [ x2 , y2 ] ,而内层的定义域实际上是包含在外层的定义域内的 ...

  5. HGU3336 Count the string (KMP Next数组的应用)

    题意:给出一个字符串,求它所有前缀在此字符串中出现的次数. 分析:一看,肯定是kmp中 next[i] 求以下标i-1结尾的字符串,使得最长的(前缀==后缀)的长度.很好想到,当next非零时,肯定和 ...

  6. bzoj-3670 [Noi2014]动物园

    3670: [Noi2014]动物园 题目链接 时间限制: 10 Sec 内存限制: 512 MB 提交: 3558 解决: 1925 [提交][][] 题目描述 近日,园长发现动物园中好吃懒做的动物 ...

  7. Java源码详解四:String源码分析--openjdk java 11源码

    文章目录 注释 类的继承 数据的存储 构造函数 charAt函数 equals函数 hashCode函数 indexOf函数 intern函数 本系列是Java详解,专栏地址:Java源码分析 Str ...

  8. 【Go】string 优化误区及建议

    原文链接: https://blog.thinkeridea.com/... 本文原标题为 <string 也是引用类型>,经过 郝林 大佬指点原标题存在诱导性,这里解释一下 " ...

  9. [LeetCode]--38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  10. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 print(string.len("hello world")) -- 11 strin ...

最新文章

  1. 利用perf排查sys高的问题
  2. arcgis ERROR:000824 该工具未获得许可
  3. 防火墙规则配置iptables
  4. PHP经常使用正則表達式汇总
  5. 关于安卓listview获得item中的控件问题
  6. Spring-Data-Redis存储对象(redisTemplate)
  7. leetcode练习--字符串中第一个唯一字符
  8. MixConv: Mixed Depthwise Convolutional Kernels
  9. python IO文件处理
  10. dbca:Exception in thread main java.lang.UnsatisfiedLinkError: get
  11. 【天梯选拔月赛】参与者人数(并查集模版题!remember find_father写法!)
  12. 莫烦python视频顺序_莫烦Python视频笔记
  13. 明解C语言中级篇练习代码------第八章
  14. 阿里云centos7系统下载
  15. 手机平板功放芯片BCT8933,PINtoPIN替换AW8733
  16. 不到最后一刻,绝不放弃!
  17. 记一次被虐的很惨的面试
  18. 解决办法在idea中搭建spark环境:Unable to fetch table student. Invalid method name: ‘get_table_req‘;
  19. 4k 显示器放大 150% 和 23寸显示器组双屏抓图问题解决
  20. SIMD、SIMD、SIMT、MISD、MIMD详解与比较

热门文章

  1. tomcat启动时报错ports are invalid,默认端口不要为-1
  2. 2019年5月,国际计量单位实施新定义
  3. 使用电脑替代人力的几个优点
  4. Python已经超过了JAVA?
  5. java数组函数_Java数组
  6. python爬虫好学不_python爬虫难学吗
  7. java-mail.jar_mail.jar-Mail.jar下载 --pc6下载站
  8. matlab ctrl c,Matlab:实现CTRL + C的功能,但在代码中
  9. java socket 连接原理_Java socket通信基本原理介绍
  10. 解决Request method 'GET' not supported问题