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. 【CUDA 基础】4.3 内存访问模式

    title: [CUDA 基础]4.3 内存访问模式 categories: - CUDA - Freshman tags: - 内存访问模式 - 对齐 - 合并 - 缓存 - 结构体数组 - 数组结 ...

  2. iOS8-Sampler

    https://github.com/shu223/iOS8-Sampler iOS8-Sampler Code examples for the new functions of iOS 8. Ho ...

  3. LeetCode---Bit Manipulation && Design

    **401. Binary Watch 思路:产生两个list分别代表小时和分钟,然后遍历 public List<String> readBinaryWatch(int num) { L ...

  4. Amaple.js框架详细介绍

    Amaple · 体验优先的JavaScript单页框架 Amaple (点此查看Github仓库)是专为单页web应用而设计的基于页面模块化的JavaScript框架,它可使开发者快速开发单页web ...

  5. JSON 转含有泛型属性的对象

    在将 json 字符串转为对象时,如果对象含有泛型,在进行转换时需要指明泛型类型. 1. 对象只含有一个泛型属性时 1.1  代码 /** * @Describe: * @Author: chenfa ...

  6. SpringBoot2.X&Prometheus使用

    1.启动Prometheus [root@ip101 prometheus-2.12.0.linux-amd64]# pwd /opt/app/prometheus-2.12.0.linux-amd6 ...

  7. AsyncTaskMethodBuilder

    AsyncTaskMethodBuilder Represents a builder for asynchronous methods that return a task. public stru ...

  8. 学习Oracle数据库入门到精通教程资料合集

    任何大型信息系统,都需要有数据库管理系统作为支撑.其中,Oracle以其卓越的性能获得了广泛的应用.本合集汇总了学习Oracle数据库从入门到精通的30份教程资料. 资料名称 下载地址 超详细Orac ...

  9. SQLite 版本引发的 Python 程序调用问题

    问题 在跑 OpenStack functional 功能测试的时候有两个用例过不去. nova.tests.functional.db.test_resource_provider.Resource ...

  10. java源码-HashMap源码分析

    这次开始分析JDK8中的HashMap源码. 首先理解HashMap中几个关键变量, TREEIFY_THRESHOLD  链表转换红黑树扩容值 table 数组+链表+红黑树  size 当前存储数 ...