hdu3336
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
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.
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.
4
abab
#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 ;
}
#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的更多相关文章
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- Count the string[HDU3336]
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU3336——KMP算法
题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头 ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
- hdu3336 kmp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string
居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- Servlet容器的启动过程
[http://book.51cto.com/art/201408/448854.htm] Tomcat的启动逻辑是基于观察者模式设计的,所有的容器都会继承Lifecycle接口,它管理着容器的整 ...
- oracle表空间建立与用户创建删除
--创建临时表空间 --//Linux下的文件系统 create temporary tablespace cloudv2_temp tempfile '/home/oracle/app/oracle ...
- 推荐第三方Oracle客户端查询工具
1.SqlDbx 官方地址:http://www.sqldbx.com/personal_edition.htm 2.devart http://www.devart.com/dbforge/orac ...
- 学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
STRUTS2 1. 写好最简单的hello world项目后,无法通过浏览器访问到hello.jsp页面,提示没有资源. 学习structs2的时间,已经更新到了2.3.16了,structs中的很 ...
- 【转】SoapUI5.0创建WebService接口模拟服务端
原文:http://blog.csdn.net/a19881029/article/details/26348627 使用SoapUI创建WebService接口模拟服务端需要接口描述文件 MathU ...
- [AngularJS] Best Practise - Minification and annotation
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...
- Swift - 初次使用:
今天Apple放出了新的编程语言.然后下载了Xcode6把系统升级到Mac OS 10.9.3 (Xcode6的系统最低要求). 创建了一个项目,折腾半天 都不知道怎么导入一个ViewControll ...
- oc-15-匿名对象
/** 匿名对象 1.访问成员变量--->只能给成员变量设置值,只能成功1次,每次都是新的对象. 2.调用方法时类似类方法: 跟类方法区别:匿名对象创建对象了,开辟空间了. */ #import ...
- 词法分析器Demo
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lexe ...
- 进程控制之fork函数
一个现有进程可以调用fork函数创建一个新进程. #include <unistd.h> pid_t fork( void ); 返回值:子进程中返回0,父进程中返回子进程ID,出错返回- ...