HDU3336-Count the string(KMP)
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4449 Accepted Submission(s): 2094
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.
1
4
abab
6
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 200000+10;
const int MOD = 10007;
char str[maxn];
int next[maxn],n;
void getNext(){
next[0] = next[1] = 0;
int ans = n;
for(int i = 1; i < n; i++){
int j = next[i];
while(j && str[i] !=str[j]) j = next[j];
if(str[j] == str[i]){
next[i+1] = j+1;
}else{
next[i+1] = 0;
}
}
}
int main(){ int ncase;
cin >> ncase;
while(ncase--){
scanf("%d%s",&n,str);
getNext();
int ans = n%MOD;
for(int i = 1; i <= n; i++){
if(next[i] !=0){
ans = (ans+1)%MOD;
}
}
printf("%d\n",ans);
}
return 0;
}
HDU3336-Count the string(KMP)的更多相关文章
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- 串string (KMP)
1.Definition 串string,是零个或多个字符组成的有限序列.一般记作S="a1a2a3...an",其中S是串名,双引号括起来的字符序列是串值:ai(1<= i ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 牛客网多校第3场Esort string (kmp)
链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...
随机推荐
- 【随记】关于List集合用Linq GroupBy分组过后的遍历小记
List<LeaderKaoQin> lstLeader = new List<LeaderKaoQin>();//一个List集合IGrouping<string, L ...
- java 优化
代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...
- InstallShield:自己备份
LIST listData;//声明listData listData = ListCreate(STRINGLIST);//创建一个空的实际字符串或数字列表. //参数都是在上个界面中赋值,然后在下 ...
- linux创建交换分区
一.SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...
- Prototype:Copy和Clone
原型模式在C#中的实现比较直接,因为只需要继承了IClone的接口,就可以通过重写Clone方法,调用MemberwiseClone()来实现ProtoType的方式. class Test:IClo ...
- spoj 4487. Can you answer these queries VI (gss6) splay 常数优化
4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...
- Android 文件上传 使用AsyncHttpClient开源框架
public void upload(View view) { AsyncHttpClient client = new AsyncHttpClient(); RequestParams reques ...
- ExecutorService介绍
转自: http://victorzhzh.iteye.com/blog/1010359 下面是excutor相关的类结果: ExecutorService接口继承了Executor接口,定义了一些生 ...
- Git命令详解
一个中文git手册:http://progit.org/book/zh/ 原文:http://blog.csdn.net/sunboy_2050/article/details/7529841 前面两 ...
- 15个值得开发人员关注的jQuery开发技巧和心得
在这篇文章中,我们将介绍15个让你的jQuery更加有效的技巧,大部分关于性能提升的,希望大家能够喜欢! 1. 尽量使用最新版本的jQuery类库 jQuery项目中使用了大量的创新.最好的方法来提高 ...