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 ...
随机推荐
- C#程序中:如何删除xml文件中的节点、元素。
C#中动态的清理xml文件中的垃圾信息是程序员必会的哦.这就像数据库一样,不会清理数据怎么可以呢?其实xml文件就可以用作一个小的数据库,存储一些简单的信息.所以,用C#程序实现xml文件的增.删.改 ...
- node.Js学习-- 创建服务器简要步骤
1.创建项目目录 mkdir ningha(文件夹名)npm init 初始化项目 获得package.json 2..在node.Js命令行操作进入到文件所在目录 3.输入browser-sync ...
- MFC笔记
一.Win32基本程序概念 所有的windows程序都必须载入windows.h MFC程序都有一个Stdafx.h文件,它载入了MFC框架必须的文件. Windows程序以消息为基础,以事件驱动之. ...
- ligerUI路径问题
ligerUI放mv的Content目录下,路径为固定的并且必须引进一下文件 <link href="~/Content/Ligerui/Source/lib/ligerUI/skin ...
- 《Journey》风之旅人;
俩个人在茫茫世界相遇,互不相识,却能互相取暖,一路旅程,看尽了美丽的风景,也共同经历了暴风雪,然而该来的人会来,该走的人会走,这不就是人生旅途?
- kafka java示例
http://www.open-open.com/lib/view/open1407942131801.html http://www.open-open.com/lib/view/open14079 ...
- 关于C语言中用Keil软件制作Lib库文件的几点经验
1.关于制止LIB库文件的几点经验 1. 一个工程如何生成lib文件: 2. 一个生成lib文件的工程可以调用这个工程中不存在的函数,只需要在.h文件中声明这些不存在函数的原型,然后在调用这个lib文 ...
- QT内置的ICON资源
QT内置的ICON资源保存在QStyle类里. 可以通过成员函数 QStyle::standardIcon 来获取. 保存的icon有: enum QStyle::StandardPixmap Thi ...
- 【POJ】2528 Mayor's posters
离散化+线段树.数组开的不够大,wa了N多回. #include <iostream> #include <cstdio> #include <cstring> # ...
- RHCS集群理论暨最佳实践
RHCS集群理论暨 最佳实践 什么是集群? 集群是一组(>2)相互独立的,通过高速网络互联的计算机组成的集合.群集一般可以分为科学集群,负载均衡集群,高可用性集群三大类. 科学集 ...