K - Count the string kmp_Next数组应用
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数组应用的更多相关文章
- HDU Count the string+Next数组测试函数
链接:http://www.cnblogs.com/jackge/archive/2013/04/20/3032942.html 题意:给定一字符串,求它所有的前缀出现的次数的和.这题很纠结,一开始不 ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- 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) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- Count the string[HDU3336]
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 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 ...
随机推荐
- 我眼中的SEO——略读一些SEO书后
近些日子一直在看一些SEO方面的书.为人有些浮躁,读SEO实在读不出太大营养,除了第一本书外,之后的书就是在不停地向后翻页.没有过太具体的实践,现在就来写一下我眼中的SEO.还希望各位多多指教. 1. ...
- 3最短路的几种解法 ------例题< 最短路 >
点击进入例题 最短路 我知道的有三种方法 1 : 深搜 每次 每次有更小的路径时 就更新 , 2 : Dijkstra 3 : floyd 前两种 是 单源 最短路径 ...
- inner join / left join / right join
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- js基础---数据类型转换
js中数据类型: 简单数据类型: number:233,-34,0x23,023 string:"hello"或者'hello' boolean:true.false undefi ...
- Ajax——php基础知识(一)
AMP环境 AMP(Apache.MySQL.PHP)是三个独立的软件,但是对于初学者而言分别安装以及配置需要掌握一定的软件知识,所以就有了很多AMP集成环境帮助我们简化安装 ——WAMP WAMP安 ...
- Struts2框架实现简单的用户登入
Struts框架汲取了Struts的优点,以WebWork为核心,拦截器,可变和可重用的标签. 第一步:加载Struts2 类库: 第二步:配置web.xml <?xml version=&qu ...
- C# Task多线程
来自Eleven老师示例 private void btnTask_Click(object sender, EventArgs e) { Console.WriteLine(); Console.W ...
- comdlg32.dll
dll的应用,目前还不知道要怎么查看dll里的功能,暂且试着用了一个, 下面的Declare 分32位office软件和64位,如果是64位,要在Declare 后面加上PtrSafe ,定义的Typ ...
- SQL几种常用的函数
函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...
- CAD插入图块前修改图块文字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...