【HDU 3336】Count the string(KMP+DP)
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数组的定义,即
\]
那么我们就可以产生一个dp方程
设dp[i]:已a[i]结尾的前缀数
\]
参考代码
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)的更多相关文章
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- 【HDU 6021】 MG loves string (枚举+容斥原理)
MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- 【HDU 5456】 Matches Puzzle Game (数位DP)
Matches Puzzle Game Problem Description As an exciting puzzle game for kids and girlfriends, the Mat ...
- 【HDU - 4340】Capturing a country(树形DP)
BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...
- 【HDU - 1069】 Monkey and Banana (基础dp)
Monkey and Banana 直接写中文了 Problem Statement 一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- 【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 ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
随机推荐
- iOS UITextView 设置圆角边框线
textView.layer.borderColor = UIColor.lightGray.cgColor textView.layer.cornerRadius = 4 textView.laye ...
- Java | 基础归纳 | Gson && Json
JSON: JSON就是一种数据的组织形式,用于数据传输. 地址:https://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4 Mav ...
- Minimal string CodeForces - 797C
Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...
- 第03课 在VMwave 14.0 上配置企业级CentOS 6.6操作系统
第一部分:配置虚拟硬件 1.1 启动VMware,选择文件-->新建虚拟机(Ctrl + N),创建一个虚拟机. (VMware的安装过程较为简单,可自行百度.) 1.2 此时,出现新建虚拟机向 ...
- DNS练习之正向解析
配置正向解析: 主机名:sishen63 主机IP:192.168.1.63 操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago) 安 ...
- Apache Kylin的架构特性
不多说,直接上干货! http://kylin.apache.org/cn/ 可扩展的超快OLAP引擎,提供标准SQL查询接口 支持单机或集群部署,为减少在Hadoop上百亿规模数据查询延迟而设计: ...
- disconf 分布式配置
摘要 为了更好的解决分布式环境下多台服务实例的配置统一管理问题,本文提出了一套完整的分布式配置管理解决方案(简称为disconf[4],下同).首先,实现了同构系统的配置发布统一化,提供了配置服务se ...
- MySQL 当记录不存在时insert,当记录存在时更新
网上基本有三种解决方法. 第一种: 示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (clie ...
- Linux之vi(vim)编辑器
命令行模式:默认进来就是命令行模式 ,可以使用很多命令:比如i . a . o i:光标前面输入内容 a:光标的下一位字符开始输入内容 o:光标的下一行开始输入内容 编辑模式: 退出编辑模式:键盘上e ...
- DELETE - 删除一个表中的行
SYNOPSIS DELETE FROM [ ONLY ] table [ WHERE condition ] DESCRIPTION 描述 DELETE 从指明的表里删除满足 WHERE 子句的行. ...