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. linux面试集

    shell:1.$# 和 $*之类的特殊变量 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n是一个数字,表示第几个参数.例如,第一个参数就是$1 $# 传递给脚本或 ...

  2. JZOJ 5914. 盟主的忧虑

    Description     江湖由 N 个门派(2≤N≤100,000,编号从 1 到 N)组成,这些门派之间有 N-1 条小道将他们连接起来,每条道路都以“尺”为单位去计量,武林盟主发现任何两个 ...

  3. 手机连上同一个局域网的PC,修改项目的vhost配置

    <VirtualHost 172.16.6.100:80> DocumentRoot "D:\phpStudy\WWW\mywork\many_school" Serv ...

  4. Python全栈day 04

    Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...

  5. bash:/usr/bin/mogod/:connot execute binary:exec fotmat error

    前两天博主在安装mogodb的时候出现以下错误,很是郁闷,明明按照教程里面做的,怎么到最后 执行命令的时候出错了呢,以下为错误execute binary:exec fotmat error" ...

  6. Kubernetes-简介(一)

    简介 Kubernetes是一个开源.用于管理云平台中多个主机上的容器化的应用,目标是让部署容器化的应用简单并且高效,Kuernetes提供了应用部署.规划.更新.维护的一种机制. 在Kubernet ...

  7. L2-032 彩虹瓶 (25 分)

    L2-032 彩虹瓶 (25 分)   彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小 ...

  8. spark的排序方法

    今天我们来介绍spark中排序的操作,spark的排序很简单,我们可以直接使用sortBy来进行,这个里面我们使用case clas,使用case class的好处是1.不用newjiukeyi 搞出 ...

  9. Dapper.Extension的基本使用

    前言    上一篇随笔写了Dapper的简单的使用,这次写一下Dapper.Extension的使用,它是Dapper的简单的封装扩展,可以通过实例化的对象赋值后进行增删改的操作以及分页,但是却不能进 ...

  10. 变量存储类型(auto static extern)

    auto 动态存储类型变量(函数内部变量存储默认为 auto型) auto只用于函数内部定义,单片机在执行这个函数时为它分配内存地址,当函数执行完毕返回后,auto变量会被销毁,再次进入这个函数时,它 ...