题目网址: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. [原创]实现android知乎、一览等的开场动画图片放大效果

    代码下载地址: https://github.com/Carbs0126/AutoZoomInImageView 知乎等app的开场动画为:一张图片被显示到屏幕的正中央,并充满整个屏幕,过一小段时间后 ...

  2. 如何改变Activity在当前任务堆栈中的顺序,Intent参数大全

    引用:http://blog.csdn.net/think_soft/article/details/7477072 本示例演示如何通过设置Intent对象的标记,来改变当前任务堆栈中既存的Activ ...

  3. WebApi中直接返回json字符串的方法

    [HttpPost] public HttpResponseMessage Upload() { string json = "{\"result\":\"tr ...

  4. [ITSEC]信息安全·Web安全培训第一期客户端安全之UBB系列

    缩略图: 引文: 所谓UBB代码,是指论坛中的替代HTML代码的安全代码.ubb发帖编辑器 这种代码使用正则表达式来进行匹配,不同的论坛所使用的UBB代码很可能不同,不能一概而论.UBB代码的出现,使 ...

  5. android自定义RadioGroup实现可以添加多种布局

    android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...

  6. 优酷、YouTube、Twitter及JustinTV视频网站架构设计笔记

    本文是整理的关于优酷.YouTube.Twitter及JustinTV几个视频网站的架构或笔记,对于不管是视频网站.门户网站或者其它的网站,在架构上都有一定的参考意义,毕竟成功者的背后总有值得学习的地 ...

  7. C头文件和源文件的连

    (http://blog.163.com/yui_program/blog/static/18415541520115177852896/) 一.源文件如何根据#include来关联头文件 1,系统自 ...

  8. oracle数据库的字符集更改

    A.oracle server 端 字符集查询  select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LAN ...

  9. SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7(问题补充)

    SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7 需要注册GAC,修改注册表 IBM官方方案: http: ...

  10. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...