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. 1391:局域网(net)

    [题目描述] 某个局域网内有n(n≤100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内的连接形成了回路,我们知道如果局域网形成回路那么数据将不停的在回路内传输,造成网络卡的现象.因为连接计 ...

  2. C. Quiz 贪心 + 数学

    http://codeforces.com/problemset/problem/337/C 题意是给出n个题目,那个人答对了m道,然后如果连续答对了k道,就会把分数double 求最小的分数是什么. ...

  3. Nginx缓存[proxy cache、memcache]

    nginx自带缓存 nginx自己有单独的进程来对磁盘上的缓存文件进行扫描,在内存中建立缓存索引.并且有管理进程来对缓存进行过期判断,更新等操作 定义:只能在http段中使用 proxy_cache_ ...

  4. cocos_js写文件

    var writablePath = jsb.fileUtils.getWritablePath(); writablePath += "cocos/data/"; var fil ...

  5. [转]利用telnet进行SMTP的验证

    本文转自:http://www.cnblogs.com/rootq/articles/1320266.html [crazywill@localhost crazywill]$ telnet #tel ...

  6. datetime 模块详解

    1.import datetime 常用方法: ttimedelta() 括号里默认为days,进行别的单位运算可以加上如hours = 1这样.除了进行减法运算,还可以进行加法运算. >> ...

  7. 洛谷 P1955 程序自动分析

    题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变 ...

  8. powerDesigner 把name项添加到注释(comment),完美方案!

    第一次写博客,分享一点经验吧,平时大家用powerDesigner的时候,pd是不会把name项默认添加到comment的,所以生成的数据库表里面也没有中文字段的注释. 我在网上查了一下.有解决方案了 ...

  9. Android(java)学习笔记166:上下文的区分

    1.两种上下文:  (1)Activity.this                               界面的上下文 (2)getApplicationContext()         整 ...

  10. es 集群部署

    下载 [root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1 ...