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 ...
随机推荐
- npm install express -g出错
npm ERR! Windows_NT npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program ...
- fedora23开发环境搭建手册
chrome安装 [安装chrome教程] nodejs环境搭建 dnf install nodejs dnf install npm sublime text 编辑器安装配置 [fedora安装su ...
- js 中文排序
/** * 比较函数 * @param {Object} param1 要比较的参数1 * @param {Object} param2 要比较的参数2 * @return {Number} 如果pa ...
- linq 多个left join 和 sql union all -> linq union 方法
( from s in Base_SysMenus join r in Base_RoleRights on s.Menu_Id equals r.Menu_Id into temp f ...
- ASP.NET MVC轻教程 Step By Step 9——分页
现在我们要把Index视图的留言信息进行分页显示. Step 1. 创建路由 我们希望以类似地址http://localhost:41583/Page1来表示第一页,Page2表示第二页,以此类推.在 ...
- code jam训练
https://code.google.com/codejam/contests.html http://student.csdn.net/mcs/programming_challenges
- 关于64位Win7/Win 8 下怎么学习汇编语言
我看有许多同学用Win 7/Win 8 学习汇编,现在好多人的内存升级了都用64位系统了,但是64位W7没有自带的DEBUG和MASM. 1.首先下载DOSBOX,(下面附带地址)它的作用就是让你在6 ...
- jquery学会的
1.$("#id") $("xxxxx") (input, body) $(".class") 2. $("#id xxx ...
- [BZOJ 1029] [JSOI2007] 建筑抢修 【贪心】
题目链接:BZOJ - 1029 题目分析 使用一种贪心策略. 现将任务按照deadline从小到大排序. 然后枚举每一个任务,如果当前消耗的时间加上完成这个任务的时间不会超过这个任务的deadlin ...
- Unicode(UTF-8, UTF-16)令人混淆的概念
为啥需要Unicode 我们知道计算机其实挺笨的,它只认识0101这样的字符串,当然了我们看这样的01串时肯定会比较头晕的,所以很多时候为了描述简单都用十进制,十六进制,八进制表示.实际上都是等价的, ...