Count the string

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1711 1686 3746 1358 3341 
TLE代码:
#include<cstdio>
#include<iostream>
using namespace std;
#define N 200010
int T,lens,lenp,nextt[N];
char s[N],p[N];
inline void get_next(){
int i=,j=-;
nextt[i]=j;
while(i<lenp){
if(j==-||p[i]==p[j]){
i++;j++;
nextt[i]=j;
}
else j=nextt[j];
}
}
inline int kmp(){
get_next();
int i(),j(),ans();
while(i<lens&&j<lenp){
if(j==-||s[i]==p[j]){
i++;j++;
}
else j=nextt[j];
if(j==lenp) ans++,j=nextt[j];
}
return ans%;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&lens);
scanf("%s",s);
int sum=;
for(int i=;i<lens;i++){
fill(p,p+i+,);
for(int j=;j<=i;j++) p[j]=s[j];
lenp=i+;
sum+=kmp();
sum%=;
}
printf("%d\n",sum%);
}
return ;
}
题解:
依次求字符串匹配数,求和,取模
水水的KMP
 
AC代码:
#include<cstdio>
#include<iostream>
using namespace std;
#define N 200010//数组开的大大的
#define mod 10007//注意取模
int T,len,Next[N];
char s[N],p[N];
inline void get_next(){//next是关键字,所以用Next[]
int i=,j=-;
Next[i]=j;
while(i<len){
if(j==-||s[i]==s[j]){
i++;j++;
Next[i]=j;
}
else j=Next[j];
}
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&len);
scanf("%s",s);
get_next();//每组数据做一次求好了,否则TLE
int sum=;
for(int i=,j;i<=len;i++){
j=i;
while(j) sum=(sum+)%mod,j=Next[j];
}
printf("%d\n",sum);
}
return ;
}

hdu3336的更多相关文章

  1. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  2. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  3. Count the string[HDU3336]

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

  4. HDU3336——KMP算法

    题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头 ...

  5. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

  6. hdu3336 kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  8. HDU3336 Count the string

    居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...

  9. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. Winform XiaoCai.WinformUI 框架界面设计

    开源用户界面和布局的套件XiaoCai.WinformUI(美化用户界面利器) http://www.cnblogs.com/aganqin/p/3400453.html 源码下载:https://g ...

  2. cocos2d-x 二进制文件的读写

    转自:http://blog.csdn.net/wolfking_2009/article/details/10616069 cocos2d-x里面的二进制文件读取的方法是有的,作者对方法封装了下,将 ...

  3. Active Low-Pass Filter Design 低通滤波器设计

    2nd order RC Low-pass Filter Center frequency    fc = 23405.13869[Hz] Q factor                  Q = ...

  4. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  5. JDBC-ODBC桥接方法连接Excel数据库的方法

    通过JDBC-ODBC桥接器访问Excel电子表格 1.设置数据源 Excel数据源选择的驱动程序是Microsoft Excel Driver 2.选择表 与访问其他数据库不同的是,我们必须在电子表 ...

  6. ARM&Linux 下驱动开发第三节

    后台驱动代码如下:比较昨天的,添加了读写指针位置移动操作 #include<linux/init.h> #include<linux/module.h> #include< ...

  7. python中使用list作为默认参数且调用时不给其赋值的问题

    最近在写代码时发现一个有趣的地方,当python中的函数使用list作为默认参数且调用时不给其赋值时,无法通过在函数中将其赋值为[]来达到清空此默认参数的目的.按照道理来说,函数f1中的list为局部 ...

  8. 【JavaScript】AJAX教程

    AJAX = Asynchronous JavaScript and XML (异步的JavaScript和XML). AJAX是与服务器交换数据并更新部分网页的技术,在不重新加载整个页面的情况下. ...

  9. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

  10. hung_task_timeout_secs 和 blocked for more than 120 seconds

    https://help.aliyun.com/knowledge_detail/41544.html 问题现象 云服务器 ECS Linux 系统出现系统没有响应. 在/var/log/messag ...