题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/A

Description

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

Input

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

Output

For 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 

题意: 给一个字符串,求这个字符串的所有前缀在字符串中出现的次数和。

思路: 使用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的更多相关文章

  1. (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 ...

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

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

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (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. HDUOJ------3336 Count the string(kmp)

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

  6. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

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

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

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

  8. Count the string -- HDOJ 3336

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

  9. HDU 3336 Count the string 查找匹配字符串

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

  10. Count the string[HDU3336]

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

随机推荐

  1. quick2.26 android下http崩溃

    quick2.26 http android下崩溃解决方案 1.先去quick官网合并代码(QuickHTTPInterface.java,CCHTTPRequestAndroid.cpp) 2.屏蔽 ...

  2. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  3. saiku 分布式实践

    saiku比较吃内存,一旦人多了,那么内存可能不够,所以会考虑主从结构,分担压力.为了保证数据的稳定性,也会有类似的考虑,那么问题来了,如何实现saiku的分布式搭建哪? 我阅读了一些国内的文章,没有 ...

  4. android 模拟器

    参考:http://www.syscs.com/node/504 --skin WVGA800 - -no-boot-anim -wipe-: the dpi for the device you a ...

  5. 现在看看自己写的博客,怎么感觉好low啊。。。

    是不会写还是怎么着? 感觉比较好的东西,难以整理,就没往上放.... 现在猛一看,博客里的东西乱七八糟的...各位看官随便看看就好

  6. Microsoft 2013 新技术学习笔记 四

    在继续学习Model的实践经验之前,先思考一下Controller和View的实践原则在本次系统重构中的应用,我手上是一个后台管理系统(不是门户系统.不是具体业务系统),通俗点讲就是给企业的运维人员用 ...

  7. 为android项目集成maven

    为什么要为android项目增加maven集成功能呢?这里我想到几个主要理由: 部署测试人员和开发人员的角色分离,让他们摆脱eclipse开发环境设置android sdk环境,直接在服务器上运行一个 ...

  8. IE代理文件自动设置

    想如果代理可用就使用代理,代理不可用就直接连接网络. 新建文件放入javascript代码,保存为proxy.pac,保存路径c:\proxy.pac function FindProxyForURL ...

  9. 字体文件放入CDN服务器中,跨域问题(IIS版)

    Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...

  10. Lua基础

    局部定义与代码块: 使用local声明一个局部变量或局部函数,局部对象只在被声明的那个代码块中有效. 代码块:一个控制结构.一个函数体.一个chunk(一个文件或文本串)(Lua把chunk当做函数处 ...