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. TensorFlow使用记录 (五): 激活函数和初始化方式

    In general ELU > leaky ReLU(and its variants) > ReLU > tanh > logistic. If you care a lo ...

  2. 顺序表应用2:多余元素删除之建表算法(SDUT 3325)

    题解: 每次询问一遍,如果已经存在就不用插入表中了. #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  3. JQuery事件和动画(7)

    一.事件: 1.鼠标事件 click( ) 触发或将函数绑定到指定元素的click事件 单击鼠标时 dblclick( ) 触发或将函数绑定到指定元素的dblclick事件 双击鼠标时 mouseov ...

  4. linux 查看内网IP和外网IP

    centos7 查看内网的ip,使用ifconfig 或在后面加上参数,都可以查看内网的ip,下面的10.105.33.17 即是内网的ip [root@VM_33_17_centos ~]#ifco ...

  5. Vue_(组件)实例方法

    Vue.js实例方法/生命周期 传送门 常用的实例方法 数据: 传送门 vm.$set:设置属性值 vm.$delete:删除属性值 vm.$watch:观测数据变化 生命周期 vm.$mount:手 ...

  6. js的5种继承方式——前端面试

    js主要有以下几种继承方式:对象冒充,call()方法,apply()方法,原型链继承以及混合方式.下面就每种方法就代码讲解具体的继承是怎么实现的. 1.继承第一种方式:对象冒充 function P ...

  7. linux出现Redirecting to /bin/systemctl start mysqld.service,解决方法

    上去就是一个命令     /bin/systemctl start httpd.service

  8. Break 和 Continue 标签 kotlin(12)

    Break 和 Continue 标签 在 Kotlin 中任何表达式都可以用标签(label ) 来标记. 标签的格式为标识符后跟 @ 符 号,例如: abc@ . fooBar@ 都是有效的标签( ...

  9. spring boot配置redis

  10. hotspot 线程状态