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 ...
随机推荐
- Jquery not选择器实现元素显示隐藏
初初认识jQuery的not选择器,想要实现的功能是,点击第一个div,显示第二个div,点击第一个div以外的地方,隐藏第二个div. 具体代码如下: <!DOCTYPE html> & ...
- 用Django搭建个人博客—(3)
今日主题 定义博客文章和评论的的数据库定义 定义操作这几个Model的后台数据 User表 USER_STATUS = ( ('active', u'激活'), ('suspended', u'禁用' ...
- 学习Swift -- 数组(Array) - 持续更新
集合类型--数组 Array是Swift中的一种集合类型:数组,数组是使用有序列表储存同一类型的多个值,与OC的NSArray的最大不同是,Swift的数组是值类型,OC的数组是引用类型 声明数组的方 ...
- winfrom拷贝文件
//File.Copy(@"C:\Users\Administrator\Pictures\bg.png", @"g:\images\bg.png", true ...
- BASLER 镜头选型白皮书
本文翻译自Basler镜头选型白皮书 有许多方法来进行镜头选型.本文将会讨论其中的指导原则,以帮助你在项目中选择合适的镜头.我们将讨论许多镜头的基本概念,比如镜头接口.图像大小.放大率.焦距.F数和光 ...
- 滴滴过节送10元打车券是不是bug
自从滴滴跟快的去年合作以后,也不玩烧钱大战了,也没法打到免费的车了,乘客打车优惠也少了. 但是现在的滴滴在过节的时候还是会返滴滴代金券,而且金额都比较大,超出了打车的起步价.半年前这边的司机会经常利用 ...
- Qt新建项目No valid kits found解决思路
Qt新建项目No valid kits found解决思路 第一次用Qt Creator创建Project时,进入Kit Selection窗口后,会提示No Valid kits found. Pl ...
- [LeetCode#246] Missing Ranges Strobogrammatic Number
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- Linux驱动的两种加载方式过程分析
一.概念简述 在Linux下可以通过两种方式加载驱动程序:静态加载和动态加载. 静态加载就是把驱动程序直接编译进内核,系统启动后可以直接调用.静态加载的缺点是调试起来比较麻烦,每次修改一个地方都要重新 ...
- [转]33 useful Keyboard Shortcuts for Run commond
原文: http://www.shortcutworld.com/en/win/Run-command.html 1. Calling Run CommandWin + r ...