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. Ubuntu16.04配置vncserver后 导致重复进入登陆界面,无法进入桌面的问题

      1.在配置vncserver的时候,可能导致该用户不能正常登录桌面. 2.问题现象:正确输入密码,系统无法进入桌面,闪回到登录界面. 3.在登录界面按ctrl+Alt+F1,进入虚拟控制台(输入r ...

  2. linux中 ls |wc -l

      linux中 ls |wc -l_百度知道 答 205行,205个字,1920个字节,wc -l只输出文件行数

  3. python hash 每次调用结果不一样

    import time import multiprocessing device = ['3695a1c7-0fa6-4fa8-a563-8fd462c04af5', '0dfdd431-f9bc- ...

  4. ReentrantLock源码探究1:非公平锁的获取和释放

    1.AQS简单介绍 ​ Sync是ReentrantLock的一个内部类,它继承了AbstractQueuedSynchronizer,即AQS,在CountDownLatch.FutureTask. ...

  5. vue-cli构建的项目结构解析

    参考: https://www.jianshu.com/p/32beaca25c0d

  6. 远程管理控制ssh

    传统的网络服务程序,FTP.POP.telnet 本质上都是不安全的,因为它们在网络上通过明文传送口令和数据,这些数据非常容易被截获.SSH叫做Secure Shell.通过SSH,可以把传输数据进行 ...

  7. pandas数据分析案例

    1.数据分析步骤 ''' 数据分析步骤: 1.先加载数据 pandas.read_cvs("path") 2.查看数据详情 df.info() ,df.describe() ,df ...

  8. 从Windows系统到Linux系统转变的5大要点

    当我在 Algoma  (阿尔格玛)大学开始我现在的工作,一个图书系统管理员,我实在是对我的工作没有什么信心.尽管我在图书信息技术上有十年经验,对于我的第一个任务——协助开发和管理 Evergreen ...

  9. 模拟SQLserver IO压力测试 工具编 SQLIOSIM

    描述 最近有业务需求需了解客户的服务器SQLserver 的IO情况,而不仅仅是通过系统计数器 了解硬盘的IO情况或者使用CrystalDiskMark或者Trace重播进行压力测试 .这时SQL S ...

  10. linux常用命令(12)head命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾.1 命令格式head [ ...