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. MFC中显示 .bmp格式的位图

    最近在看VisualC++ 图像处理的书籍,表示一直在从基础做起,今天就记录一个简单功能的实现,显示.bmp格式的位图. 首先需要理解的是窗口创建的过程包括两个步骤:首先擦除窗口的背景,然后在对窗口进 ...

  2. HttpResponse类

    HttpReponse是服务器接收到浏览器的请求后,处理返回结果常用的一个类. 一.属性 Buffer 获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它. BufferOutput ...

  3. SAP MRP的计算步骤

          SAP MRP的计算步骤,物料需求计划(简称为MRP)与主生产计划一样属于ERP计划管理体系,它主要解决企业生产中的物料需求与供给之间的关系,即无论是对独立需求的物料,还是相关需求的物料, ...

  4. win7远程桌面连接

    远程桌面连接 1.在防火墙上设置同意同意远程桌面通过防火墙: 2.为管理员设置password.以便訪问者訪问: 3.我的电脑-右键属性-远程设置-远程-选上"同意远程协助远程这台计算机.同 ...

  5. Android Checkbox Example

    1. Custom String 打开 “res/values/strings.xml” 文件, File : res/values/strings.xml <?xml version=&quo ...

  6. JAVA+FlexPaper+OpenOffice+SWFTools文档预览

    http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.ex ...

  7. WCF关于VS2010的配置

    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 下的 devenv.exe.config 在 <configura ...

  8. 架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层

    1.概述 前两遍文章中,我们一直在说后文要介绍Nginx + Keepalived的搭建方式.这篇文章开始,我们就来兑现前文的承诺,后续的两篇文章我们将介绍Nginx + Keepalived和 LV ...

  9. 帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET

    帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET 电视信号是通过摄像机对自然景物的扫描并经光电转换形成的.扫描方式分为“逐行扫描”和“隔行扫描”.“逐行扫描”指每幅图像均是由电子 ...

  10. HTTP权威指南学习心得

    一.HTTP请求的步骤: 1.从url中读取主机名 2.利用DNS(domain name service)对主机名进行转换,得到IP地址 3.如果有端口号的话,读取端口号 4.根据IP地址和端口号, ...