Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11607    Accepted Submission(s): 5413
Problem 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.

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

Author

foreverlin@HNU

Source

HDOJ Monthly Contest – 2010.03.06

Recommend

lcy

题解:

      ①首先发现如果长度为i前缀出现了,那么长度小于i的前缀也就随之出现了至少一次。

      ②考虑KMP中的next数组表示了最长前后缀的性质,可以使用如下DP:
             num[i]表示序列a[0~i]中出现了好多个前缀(不论长度)

             转移方程式:num[i]=num[next[i]]+1 (+1是自己匹配自己)

     ③这样做的原因:next指向最长前缀,那么相同的当前后缀可以再构造一模一样的解。

     ④至此发现其实num[i]的定义是有缺陷的,只是为了方便理解。因为为了不重复计算答案,必须不能保存以前的答案(即答案不是num[m])。

#define M 10007
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=200003;int T,m,j,f[N],num[N],ans;char P[N];
int main()
{
scanf("%d",&T);
while(ans=0,T--&&scanf("%d%s",&m,P))
{
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
go(i,1,m)(ans+=((num[i]=num[f[i]]+1)%=M))%=M;
printf("%d\n",(ans%M+M)%M);
}
return 0;
}//Paul_Guderian

Turns out,real life's a little bit more complicated than a slogan on a bumper sticker……

                                                                         ————Judy·Hopps

【HDU 3336 Count the string】的更多相关文章

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

  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(next数组运用)

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

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

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

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

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

  6. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  8. ACM hdu 3336 Count the string

    [题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...

  9. HDU 3336——Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. vue组件封装及父子组件传值,事件处理

    vue开发中,把有统一功能的部分提取出来,作为一个独立的组件,在需要使用的时候引入,可以有效减少代码冗余.难点在于如果封装,使用,如何传参,派发事件等,我会采取倒叙的方式进行说明.(本文总结于Vue2 ...

  2. xml中Node和Element的区别

    本文转载自:http://blog.csdn.net/wcydiyi/article/details/4432636点击打开链接 1.元素(Element)和结点(Node)的区别:         ...

  3. 为 vsftpd 启动 vsftpd:500 OOPS: SSL: cannot load RSA&nb

    博主在配置ftp服务器的时候 自己生成的CA,然后认证自己的私钥文件 一切配置都是没有问题的,然后重新启动服务器 service vsftpd restart    出现以下错误 为 vsftpd 启 ...

  4. 一些 Markdown 语法

    参考于: https://www.jianshu.com/p/b03a8d7b1719 [先挖个坑,来日再填]

  5. Python 装饰器执行顺序迷思

    Table of Contents 1. 探究多个装饰器执行顺序 1.1. 疑问 1.2. 函数和函数调用的区别 1.3. 装饰器函数在被装饰函数定义好后立即执行 1.4. 疑问的解释 2. 参考资料 ...

  6. Android 游标

    静下心来,学一下Android的数据库连接. 1.直接从getReadableDatabase()与getWritableDatabase()入手.    --getReadableDatabase( ...

  7. python ranndom模块及生成验证码

    python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...

  8. 多路复用IO模板

    服务端 from socket import * import select server = socket(AF_INET, SOCK_STREAM) server.bind(('127.0.0.1 ...

  9. Android getLocationInWindow

    参考博客: http://blog.sina.com.cn/s/blog_44d19b500102vpve.html 这篇博客,我看了三遍,终于看懂了.恩,我就喜欢这样不放弃的自己. 1.getLoc ...

  10. Java中数据类型转换&基本类型变量和对象型变量

    1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型    byte,short,int,lo ...