It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
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的更多相关文章

  1. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  2. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  3. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  4. 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 ...

  5. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. 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 ...

  8. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  9. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. Liblinear Visual studio 2013 Error C3057

    使用LibLinear时编译时出现Error C3057的错误: OpenMP报错,查询MSDN: #pragma omp threadprivate(var)//var在编译之前必须是确定值 所以修 ...

  2. Spring——bean的五种作用域和生命周期

    一.Bean的作用域 1.当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回 ...

  3. Apache 服务器 首次访问特别慢的解决过程,php环境

    一台服务器之前装的是java的tomcat apache 项目, 后面装了个phpstudy 在上面,访问php项目发现 浏览器首次打开网页需要7-8秒,打开成功后连续访问都很快,过一段时间访问又是7 ...

  4. JavaWeb_(Struts2框架)使用Servlet实现用户的登陆

    JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆 传送门 JavaWeb_(Struts2框架)Servlet与Struts区别 传送门 MySQL数据库中存在Gary用户,密 ...

  5. Django 详解

    Django是一个开源的Web应用框架,由Python写成.采用MVC的软件设计模式,主要目标是使得开发复杂的.数据库驱动的网站变得简单.Django注重组件的重用性和“可插拔性”,敏捷开发和DRY法 ...

  6. Java集成POI进行Excele的导入导出,以及报错: java.lang.AbstractMethodError..........

    报错信息如下 java.lang.AbstractMethodError: org.apache.poi.xssf.usermodel.XSSFCell.setCellType(Lorg/apache ...

  7. 梯度下降(Gradient Descent)小结 -2017.7.20

    在求解算法的模型函数时,常用到梯度下降(Gradient Descent)和最小二乘法,下面讨论梯度下降的线性模型(linear model). 1.问题引入 给定一组训练集合(training se ...

  8. 反射 go

    reflect.Valueof 到底是个什么? 反射值对象(reflect.Value)提供一系列方法进行零值和空判定,如下表所示. 反射值对象的零值和有效性判断方法 方 法 说 明 IsNil() ...

  9. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  10. Linux安全加固之中间件Tomcat

    (注:皆为参考操作配置) 这次是tomcat的Linux加固,分为身份鉴别.访问控制.安全审计.资源控制和入侵防范5个方面大部分加固基于xml配置文件进行修改,也应根据实际需求制定方案.寻找配置文件目 ...