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. Mac OS X取消Apache(httpd)开机启动

    安装MAMP后,启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache,可是默认是没有启动的.我也没有开启Web共享,怎么就开机启动了呢? 不知 ...

  2. iOS 在任意界面 Dismiss Keyboard

    最近由于项目需要,有些时候我们需要在任意时刻dismiss掉键盘. 很自然的我们会想到键盘通知 UIKeyboardDidShowNotification和UIKeyboardDidHideNotif ...

  3. ExtJS和AngularJS比较

    原文地址:http://www.techferry.com/articles/ExtJS-vs-AngularJS.html ExtJS和AngularJS比较.pdf          

  4. python flask model 序列化

    class DictSerializable(object):     def as_dict(self,*args):         result = OrderedDict()         ...

  5. 【WebForm】Js调用后台C#方法

    因业务的需要,有这么个需求,需要前台的JS传参调用C#后台的方法.现在有这么个方法可以解决,整理如下. 首先,先说一下基本实现,前台用Jquery的ajax将其中的URL后加方法,然后在Data中传递 ...

  6. linux scp 服务器远程拷贝

    一.将本机文件复制到远程服务器上 #scp /home/administrator/news.txt root@192.168.1.1:/etc/squid /home/administrator/  ...

  7. .net中怎么使用CKEditor

    1:官网下载Full Package2:将此ckeditor文件包拷贝到项目根目录下3:CKEditor 3.6.6.2 for ASP.NET下载4:复制_Samples\bin\Release\C ...

  8. Editor Scripting学习笔记之Menu Item

    主要用到: MenuItem属性 MenuCommand参数 可能用到: EditorApplication类 Selection类 GameObjectUtility类 FileUtil类 Undo ...

  9. TP复习16

    'APP_GROUP_LIST' => 'Home,Admin', //项目分组设定'DEFAULT_GROUP' => 'Home', //默认分组 开分组复制上面 AjaxReturn

  10. Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象

    Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象 package org.rui.collection2.map; /** * ...