博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK - Count the string HDU - 3336(前缀数量问题)...
阅读量:4557 次
发布时间:2019-06-08

本文共 1719 字,大约阅读时间需要 5 分钟。

K - Count the string HDU - 3336

题目链接:

题目:

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
14abab
Sample Output
6 题意:给你一个字符串,求出所有前缀的个数和,可以重叠 思路:首先前缀个数就是字符串长度len,然后利用kmp求next数组,如果next[n]为4,对应前后缀最长相等的部分为abcd,那么a,ab,abc,abcd,都要加一次就是加next【n】,所以这题就是相当于 求next数组,只要满足next[i]+1!=next[i+1]就行,就可以找到所有前缀的个数和了
// // Created by HJYL on 2019/8/16.//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int maxn=1e6+10;char str[maxn];int nextt[maxn];void getnext(){ int i=0,j=-1; nextt[0]=-1; int len=strlen(str); while(i

 

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11366768.html

你可能感兴趣的文章
关于8.0.15版本的mysql下载与安装
查看>>
Redis主从复制看这篇就够了
查看>>
洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
查看>>
(4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧...
查看>>
基本数据类型(数字和字符串)
查看>>
函数__装饰器
查看>>
linux system函数分析
查看>>
前端优化措施
查看>>
论学习汉语和学习编程的异同点
查看>>
linux img文件压缩及解压
查看>>
Linux 下的 scp
查看>>
理解同步,异步和延迟脚本
查看>>
MMS源码中异步处理简析
查看>>
XMind 6 如何画流程图
查看>>
final发布评价
查看>>
DLL远程注入与卸载
查看>>
Jmeter-ForEach控制器
查看>>
Checklist: 2019 05.01 ~ 06.30
查看>>
Binary XML file : Error inflating class com.esri.android.map.MapView
查看>>
grep,awk和sed
查看>>