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

题解


考虑kmp算法中next数组的定义,即

\[f[i]=k\;whilea[0....k]==a[k+1...j]
\]

那么我们就可以产生一个dp方程

设dp[i]:已a[i]结尾的前缀数

\[dp[i]=dp[f[i]]+1
\]

参考代码

import java.io.*;
import java.util.*; public class Main{
static int N=200000+10;
static int f[]=new int [N];
static char a[]=new char[N];
static void getFail(char b[],int m) {
int j=0;
f[1]=0;
for(int i=2;i<=m;i++) {
while(j>0&&b[j+1]!=b[i]) j=f[j];
if(b[j+1]==b[i]) j++;
f[i]=j;
}
}
static int dp[]=new int[N];
public static void main(String[] args){
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T--!=0) {
int n=in.nextInt();
String str=in.next();
for(int i=0;i<n;i++) a[i+1]=str.charAt(i);
getFail(a,n);
Arrays.fill(dp, 0);
int ans=0;
for(int i=1;i<=n;i++) {
dp[i]=(dp[f[i]]+1)%10007;
ans+=dp[i];
ans%=10007;
}
out.println(ans);
out.flush();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}

【HDU 3336】Count the string(KMP+DP)的更多相关文章

  1. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  2. 【HDU 6021】 MG loves string (枚举+容斥原理)

    MG loves string  Accepts: 30  Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  3. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  4. 【HDU 5456】 Matches Puzzle Game (数位DP)

    Matches Puzzle Game Problem Description As an exciting puzzle game for kids and girlfriends, the Mat ...

  5. 【HDU - 4340】Capturing a country(树形DP)

    BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...

  6. 【HDU - 1069】 Monkey and Banana (基础dp)

    Monkey and Banana 直接写中文了 Problem Statement 一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子 ...

  7. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  8. 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

随机推荐

  1. java 丢失的异常

    采用finally从句中的,可能会丢失异常 package thinking; //: LostMessage.java // How an exception can be lost class V ...

  2. redis过期策略+事务+分布式锁+单redis服务器锁

    过期策略 相关知识:redis 内存数据集大小上升到一定大小的时候,就会施行数据淘汰策略.redis 提供 6种数据淘汰策略: voltile-lru:从已设置过期时间的数据集(server.db[i ...

  3. BZOJ5484(LIS性质+树状数组)

    题目传送 学习的这篇题解. 结论: 1.直观感受一下会发现找到LIS,LIS里的东西相对位置是不会变的,其他的移一移总会排序成功的,所以其他的就是最小集合了,第一问的答案就是n-LIS: 2.寻找字典 ...

  4. Throwing Dice LightOJ - 1064 || (勉强能用的)分数类

    Throwing Dice LightOJ - 1064 方法: 设ans[i][j]表示i个骰子点数恰好为j的概率.那么ans[1][1]到ans[1][6]都为1/6. 显然,$ans[i][j] ...

  5. Jury Meeting CodeForces - 854D

    Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体 ...

  6. h5-27-存储/读取JS对象

    存储JS对象 <script type="text/javascript"> /*封装人员信息*/ function Person(id,name,age) { thi ...

  7. Elasticsearch (1) - 索引库 文档 分词

    创建索引库 ES的索引库是一个逻辑概念,它包括了分词列表及文档列表,同一个索引库中存储了相同类型的文档.它就相当于MySQL中的表,或相当于Mongodb中的集合. 关于索引这个语: 索引(名词):E ...

  8. Spring数据访问1 - 数据源配置及数据库连接池的概念

    无论你要选择哪种数据访问方式,首先你都需要配置好数据源引用. Spring中配置数据源的几种方式 通过在JDBC驱动程序定义的数据源: 通过JNDI查找的数据源: 连接池的数据源: 对于即将发布到生产 ...

  9. SQL中的SELECT_简单查询语句总结

    --以scott用户下的dept和emp表为例 --注意:如果scott用户不能使用,请使用system用户登录--解锁scott用户ALTER USER SCOTT ACCOUNT UNLOCK;- ...

  10. 在自己的工程中使用ijkplayer的功能

    最近在做一个软解视频叠加硬解视频的方案,网上看了很多教程,始终不得要领.虽然ijkplayer提供了ijkplayer-example这个示例工程,但对于初入安卓的人来说,要将ijkplayer整合到 ...