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

一个结论:cnt[i] 表示 因为加入i字符而增加的 以i字符结束的前缀数目
cnt[i] = cnt[Next[i]] + 1
Next[]数组表示前j个字符中最长的相同前缀后缀长度
证明:
如果Next[i]==0 也就是说 i字符没有在之前出现过,那么因为加入字符i增加的前缀只有0-i这个字符串
否则 增加的数目要考虑由于第i-k个字符到第i个字符组成的字符串是前缀的情况,增加的数目为这种前缀的数目+1,那么这种前缀的数目如何求?
显然,考虑i之前最长匹配的前缀后缀,在最长匹配处加入字符i增加的前缀 等于 在i处加入字符i增加的前缀
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 200002
#define INF 0x3f3f3f3f
#define M 10007
typedef long long LL;
/*
输出字符串中所有前缀在字符串中出现的次数
ababab
*/
char t[MAXN];
int Next[MAXN],cnt[MAXN];
void kmp_pre(char t[])
{
int m = strlen(t);
int j,k;
j = ;k = Next[] = -;
while(j<m)
{
if(k==-||t[j]==t[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int l,ans=;
scanf("%d",&l);
scanf("%s",t);
kmp_pre(t);
for(int i=;i<=l;i++)
{
cnt[i] = (cnt[Next[i]]+);
ans = (ans+cnt[i])%M;
}
printf("%d\n",ans);
}
}

K - Count the string kmp_Next数组应用的更多相关文章

  1. HDU Count the string+Next数组测试函数

    链接:http://www.cnblogs.com/jackge/archive/2013/04/20/3032942.html 题意:给定一字符串,求它所有的前缀出现的次数的和.这题很纠结,一开始不 ...

  2. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  4. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 2020牛客暑期多校训练营(第四场) C - Count New String (字符串,广义后缀自动机,序列自动机)

    Count New String 题意: 定义字符串函数 \(f(S,x,y)(1\le x\le y\le n)\),返回一个长度为y-x+1的字符串,第 i 位是 \(max_{i=x...x+k ...

  7. Count the string[HDU3336]

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

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

随机推荐

  1. 湖南集训Day1

    难度 不断网:☆☆☆ 断网:☆☆☆☆ /* 卡特兰数取模 由于数据范围小,直接做. 考试时断网.忘记卡特兰数公式,推错了只有5分. 数学公式要记别每次都现用现搜!!! */ #include<i ...

  2. [转]mysql触发器的作用及语法

    转自:http://blog.csdn.net/cloudday/article/details/6905590 触发器是一种特殊的存储过程,它在插入,删除或修改特定表中的数据时触发执行,它比数据库本 ...

  3. [转]c# 对密码执行散列和 salt 运算方法

    本文转自:http://www.cnblogs.com/CnBlogFounder/archive/2008/07/04/1235690.html 大家对密码执行散列和Salt运算一定不陌生.两个Vi ...

  4. 数字签名与数字证书以及https

    数字签名与数字证书以及httpshttps://blog.csdn.net/lzghxjt/article/details/79604602

  5. Windows下apache+tomcat负载均衡

    Windows下apache+tomcat负载均衡 网上已经有很多的资料,但是很多都比较零碎,需要整合一起才能搭建出理想的负载均衡,正好前段时间搭建了windows与linux下的负载均衡,在此记录, ...

  6. asp.net——统计输入的字符数目

    asp.net——统计输入的字符数目 题目: 在页面中有一个TextBox输入框,一个显示文字用的Label,一个提交按钮Button.在TextBox中输入一段英文字母,点击按钮提交后统计其中字母‘ ...

  7. JS——delete

    1.对象属性删除 <script> function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.nam ...

  8. CSS——ul(demo)

    1.ul本身是块级元素,在实际运用中,我们不设定宽高的话,它的宽就是父元素的宽,它的高就是内容撑起来的高度. 2.在局部布局的时候,我们可以不用设定ul的宽度和高度,直接使用margin来巧妙布局. ...

  9. Js配置资料下载

    1.使用windows.loaction.href链接下载: 此种下载在本页打开,eg:windows.location.href = http://www.xxx.xx/aa.apk; 2.使用wi ...

  10. web移动端适配

    /*** html节点字体大小随屏幕大小改变 用于rem布局***/首先这是一个立即执行函数(function (doc, win) { var docEl = doc.documentElement ...