KMP---Count the string
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/A
Description
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.
Input
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.
Output
Sample Input
1
4
abab
Sample Output
6 题意: 给一个字符串,求这个字符串的所有前缀在字符串中出现的次数和。 思路: 使用next[]跳转表,已经有了next数组,next[i]=k表示最大的j使得0~k-1==i-k+1~i,因此,对于样例abab,则有
0 1 2 3
s[] a b a b
p[] 0 0 1 2
对于4个前缀:
a
ab
aba
abab
设dp[i]表示子串s[0~i]共含有以s[i]为结尾的前缀的数目,则以s[i]结尾的前缀数就是自己本身加上以s[p[i]]结尾的前缀数,也就是例如i=2
则有:p[i]=1,dp[i]=dp[p[i]-1]+1 ;
a
aba这两个前缀,其中a就是s[p[i]]结尾的前缀。
本题代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
char s[];
int p[];
int dp[];
int n; int next_()
{
int k=;
int sum=;
int len=strlen(s);
p[]=;
dp[]=;
for(int i=;i<len;i++)
{
while(k>&&s[k]!=s[i])
k=p[k-];
if(s[k]==s[i])
k++;
if(k>) dp[i]=(dp[k-]+)%;
else dp[i]=;
sum=(sum+dp[i])%;
p[i]=k;
}
return sum;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%s",&n,s);
printf("%d\n",next_());
}
return ;
}
KMP---Count the string的更多相关文章
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- 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 ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (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) ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
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) ...
- Count the string -- HDOJ 3336
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Count the string[HDU3336]
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- mac os x 安装mysql遇到 Access denied for user 'root'@'localhost' (using password: YES)的解决方法
最近把开发环境迁移到macbook上,装上mysql启动之后,连接,总是报这个错误.5.5的版本默认密码为空.其实这个错误是root未授权的问题.解决方法如下: 未给localhost root用户授 ...
- 通过PowerShell获取Windows系统密码Hash
当你拿到了系统控制权之后如何才能更长的时间内控制已经拿到这台机器呢?作为白帽子,已经在对手防线上撕开一个口子,如果你需要进一步扩大战果,你首先需要做的就是潜伏下来,收集更多的信息便于你判断,便于有更大 ...
- Raft论文的一些问题
抛些问题出来,真正解释了这些问题才算理解了论文.:) 1. 什么是复制状态机 2. Raft vs Paxos 3. Raft的设计目标understandability,为达到设计目标在做设计时如何 ...
- CAS认证原理图
- merge 实现
今天写了个小程序,做两个已经从小到大排序好的数据的merge. 要求: listA = (1, 3, 5, 10); listB = (4, 6, 12):listA 和listB都是排序由小到大的列 ...
- Form personization(Form 个性化)报无权限
总部的同事利用form personization对工单的一些Form做了个性化,发现可能设的有问题,造成用户无法关工单.想要看一下她是怎么设的,可报没权限.经过研究发现,把个人Profile 的 U ...
- js常见怪异
1.隐式转换为布尔:"truthy"和"falsy" 当 JavaScript 需要一个布尔值时(例如:if 语句),任何值都可以被使用. 最终这些值将被转换为 ...
- ruby类名之间<,<=方法
有时候看源码的时候看到类名之间存在<.<=操作,顿时一头雾水,类名之间也可以比较吗?查了下api,豁然开朗 Class的父类是Module,Module.methods.grep(/< ...
- 封装Js事件代理方法
// 封装事件代理 function delegateEvent(element, tag, event, listener) { // 判断是否支持addEventlistener if(eleme ...
- 【网站开发】在新浪SAE上搭建一个博客
概述 在新浪SAE上搭建一个博客 1.访问新浪SAE站点 http://sae.sina.com.cn/ 2.注册新浪SAE 3.选择应用仓库 4.选择WordPress 5.安装WordPress ...