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. SpringMVC注解@RequestParam解析

    1.可以对传入参数指定参数名 1 @RequestParam String inputStr 2 // 下面的对传入参数指定为param,如果前端不传param参数名,会报错 3 @RequestPa ...

  2. glibc2.12升级至2.15

    1.操作系统版本 [root@localhost ~]# cat /etc/redhat-release #CentOS release 6.9 (Final) 2.当前glibc版本 [root@l ...

  3. intellij idea 添加动态 user library(java.lang.VerifyError)【转】

    使用IDEA的时候有时要用到eclipse的user library,由于两个IDE导入library的方式不同导致我们找不到导入user library的方法. 查IDEA的官方文档,找到方法如下: ...

  4. php面向对象(2)值传递

    PHP中值传递方式,2中 值传递:传递的时候,拷贝的是数据本身.默认都是值传递 结果:传递完成,有了2份同样的数据,且2个变量“相互独立”,不会相互影响 引用传递:传递的时候,拷贝的是引用关系(数据的 ...

  5. JZOJ| 5910. DuLiu

    Description          LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃圾题目者也,教人半懂不懂的.因为他姓李,别人便从QQ群上的“毒瘤李F ...

  6. python-10多进程

    1-多进程(multiprocessing), 1个父进程可以有多少子进程 1.1下面的例子演示了启动一个子进程并等待其结束 from multiprocessing import Process i ...

  7. 3 Mongodb数据查询1

    1.基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty():将结果 ...

  8. laravel5.5artisan命令

    目录 1. 简介 2. 编写命令 2.1 构建自己的命令 2.2 闭包命令 3. 定义输入期望 4.I/O 命令 5. 注册命令 6. 调用命令 1. 简介 Artisan 是 Laravel 自带的 ...

  9. 剑指Offer - 九度1351 - 数组中只出现一次的数字

    剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...

  10. DOS程序员手册(十五)

    837页 writeln('TRACING Current Buffer==='); holdup; bcbtrc(cvtbase^.curbfr); writeln; holdup ; writel ...