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 ...
随机推荐
- yum update Transaction Check Error
update系统时,发现其中一台server居然提示: Transaction Check Error:file /usr/lib/perl5/5.8.8/CGI.pm from install of ...
- Linux 中使用 KVM
from:http://www.php-oa.com/2010/02/22/ubutnu-kvm-vmware.html 听讲XEN很快就要从LINUX内核中去掉,redhat和Ubuntu也大力的在 ...
- 【原创】[PS技巧]如何制作翘角纸条的阴影效果
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-05-19) 在网页设计中经常会用到翘角纸条的效果.如:百度空间首页: pic.1 下面我详细讲解一下这个效果使用PS如何制 ...
- Java学习笔记之方法重载,动态方法调度和抽象类
一.方法重载 如果子类中的方法与它的超类中的方法有相同的方法名,则称子类中的方法重载超类中的方法,特别是当超类和子类中的方法名和参数类型都相同时,在子类中调用该方法时,超类中的方法会被隐藏.考虑下面程 ...
- [Ramda] Filter, Reject and Partition
We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...
- $parse/$eval和$observe/$watch如何区分
大家在看angular的时候,有时候偶尔会看到$parse,$eval和$observe,$watch这两对语法,随着深入使用angular,就不可避免使用到它.文章从内部运行机制跟实际需求的角度来解 ...
- cocos2dx-jsb 跨语言调用及第三方集成 - 过程记录
1:C++中调用js方法: 问题:ios中当用户通过home键将游戏转入后台时,调用js中的暂停游戏方法: AppDelegate::applicationDidEnterBackground() 和 ...
- THD 变量存入threads中
http://blog.csdn.net/gapaul/article/details/12047497 http://ourmysql.com/archives/930
- 认清Linux中标准输入和标准输出的双重含义
按照惯例,UNIX系统shell使用文件描述符0与进程的标准输入(一般是键盘)相关联,文件描述符1与标准输出(一般是显示器)相关联,文件描述符2与标准出错输出(一般是显示器)相关联. 在依从POSIX ...
- 话说C语言const用法
const在C语言中算是一个比较新的描述符,我们称之为常量修饰符,意即其所修饰 的对象为常量(immutable). 我们来分情况看语法上它该如何被使用. 1.函数体内修饰局部变量. 例: void ...