HDU-3366-Count the string(KMP,DP)
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14368 Accepted Submission(s): 6572
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匹配的话,会超时,所以我们要利用KMP的特性来巧妙计算。
- KMP中的next数组表示的是最长公共前后缀字串长度,基于这一个特点。每次遍历到 i ,对于 next[i] 来讲,又重复出现了前缀长度为next[i] 的字串,基于这一特点,我们由前向后推出每一个 i 处的所有前缀重复数,所谓DP。
dp[i] = (dp[nxt[i]]+1)%mod;
即状态转移方程。也就是说,长度为 i 的前缀串中又多了一份长度为nxt[i]的前缀字串的个数,再加上自身,就是更新得到的数量。
const int mod = 10007;
const int MAX = 200005;
int dp[MAX];
int n;
int nxt[MAX];
char a[MAX];
void getnxt()
{
nxt[0] = -1;
int j = 0,k=-1;
while(j<n)
{
if(k==-1||a[j]==a[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>n;
cin>>a;
getnxt();
long long ans = 0;
for(int i=1;i<=n;i++)
dp[i] = (dp[nxt[i]]+1)%mod;
for(int i=1;i<=n;i++)
ans = (ans+dp[i])%mod;
cout<<ans<<endl;
}
return 0;
}
HDU-3366-Count the string(KMP,DP)的更多相关文章
- HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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) ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4588 Count The Carries (数学,计数)
题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...
- HDU 5414 CRB and String (字符串,模拟)
题意:给两个字符串s和t,如果能插入一些字符使得s=t,则输出yes,否则输出no.插入规则:在s中选定一个字符c,可以在其后面插入一个字符k,只要k!=c即可. 思路:特殊的情况就是s和t的最长相同 ...
- string (KMP+期望DP)
Time Limit: 1000 ms Memory Limit: 256 MB Description 给定一个由且仅由字符 'H' , 'T' 构成的字符串$S$. 给定一个最初为空的字符串 ...
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
随机推荐
- ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(约束第K短路)
题意:求11到nn的第kk短的路径长度,如果超过TT输出Whitesnake!Whitesnake!,否则输出yareyaredawayareyaredawa. 好无以为 , 这就是一道模板题, 当是 ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- Vue 5 -- axios、vuex
一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...
- unity ForceMode
public float jumpAbility; GetComponent<Rigidbody>().AddForce(Vector3.up * jumpAbility, ForceMo ...
- (转)CentOS系统启动流程图文详解
CentOS系统启动流程图文详解. 原文:http://www.linuxidc.com/Linux/2017-03/141966.htm 熟悉系统启动流程对于我们学习Linux系统是非常有帮助的,虽 ...
- Android图表库XCL-Charts
首先,这个是国人开发的,支持下必须顶!github项目地址:点击打开,由于项目的基本功能已经实现,所以项目作者也说以后基本不会在有更新了. 项目简介: Android图表库(XCL-Charts is ...
- 【踩坑】遇到 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 报错
今天在重做 iblog 客户端时,测试接口情况,发现了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...
- Java调用SQL Server的存储过程详解(转)
1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序列.不带参数的 call 转义序列的语法如下所示: 以下是引用片段:{call proce ...
- javascript浮点值运算舍入误差
问题 在javascript中整数和浮点数都属于Number数据类型(简单数据类型中的一种),我们经常会发现在打印1.0这样的浮点数的结果是1而非1.0,这是由于保存浮点数的内存空间是保存整数值的两倍 ...
- web端 css hack(一)
逢10月小长假,几天不敲键盘,浑身难受.也是有时间分享一下自己遇到的css问题.先说一下什么css hack 简单介绍一下css hack: 定义: 一般都是利用各浏览器的支持CSS的能力和BUG来进 ...