HDU3336 Count the string(kmp
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.
InputThe first line is a single integer T, indicating the number of test cases.
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.
OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input
1
4
abab
Sample Output
6
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1e6+;
const int mod = 1e4+;
int net[maxn],len;
char c[maxn];
void getnext(){
int l = strlen(c);
int i = ,j = -;
net[]=-;
while(i<l){
if(j==-||c[i]==c[j]) net[++i]=++j;
else j = net[j];
}
}
int main()
{
int t;
cin>>t;
while(t--){
cin>>len>>c;
getnext();
int l = strlen(c),res = ;
for(int i = ;i < l;++i){
if(net[i]!=&&net[i]+!=net[i+])res += net[i];
}
cout<<(res+l+net[l])%mod<<endl;
}
return ;
}
HDU3336 Count the string(kmp的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- 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 KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
随机推荐
- 全网最!详!细!tarjan算法讲解。——转载自没有后路的路
全网最!详!细!tarjan算法讲解. 全网最详细tarjan算法讲解,我不敢说别的.反正其他tarjan算法讲解,我看了半天才看懂.我写的这个,读完一遍,发现原来tarjan这么简单! tarj ...
- TensorFlow使用记录 (六): 优化器
0. tf.train.Optimizer tensorflow 里提供了丰富的优化器,这些优化器都继承与 Optimizer 这个类.class Optimizer 有一些方法,这里简单介绍下: 0 ...
- git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
在同步本地文件到线上仓库的时候 报错 pre -commit hook failed (add --no-verify to bypass) 当你在终端输入git commit -m "xx ...
- 修改jupyter notebook的字体等样式
方法一 /lib/site-packages/notebook/static/custom/ 里面有个custom.css文件,你只要修改这个文件就可以了. /* jupyter notebook中显 ...
- How to correctly set application badge value in iOS 8?
o modify the badge under ios8 you have to ask for permissions let settings = UIUserNotificationSetti ...
- 2.1 MATLAB的数据类型
2.1 MATLAB的数据类型 每种数据类型都是以矩阵的形式存在的 数据类型:数值型.字符型.元胞型.结构体.函数句柄 数值型包含:双精度类型.单精度类型.整型 支持不同数据的转换 2.1.1 变量与 ...
- cdn and fallback
https://www.davepaquette.com/archive/2015/05/06/link-and-script-tag-helpers-in-mvc6.aspx It is a com ...
- SpringSecurity——默认过滤器链
介绍Spring Security默认的过滤器链,介绍顺序按照过滤器在过滤器链中的顺序排序 1.WebAsyncManagerIntegrationFilter 将Security上下文与Spring ...
- java html table 转 excel,给予jdom 和 poi
maven 引入 <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artif ...
- py matplotlib 多个figure同时画多个图以及多个图例多个折线图
图例负号乱码的问题 import numpy as np import matplotlib.pyplot as pltimport matplotlibplt.rcParams['axes.un ...