(KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6672 Accepted Submission(s): 3089
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.
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.
| 1 | 2 | 3 | 4 | 5 | |
| a | b | a | b | a | |
| -1 | 0 | 0 | 1 | 2 | 3 |
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define MOD 10007
#define N 210000 char M[N];
int Next[N], dp[N]; void FindNext(int len)
{
int i=, j=-;
Next[] = -; while(i<len)
{
if(j==- || M[i]==M[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, i, Sum=;
memset(dp, , sizeof(dp)); scanf("%d%s", &n, M); FindNext(n);
for(i=; i<=n; i++)
{
dp[i] = (dp[Next[i]] + )%MOD;
Sum = (dp[i]+Sum)%MOD;
} printf("%d\n", Sum);
}
return ;
}
(KMP)Count the string -- hdu -- 3336的更多相关文章
- Count the string HDU - 3336
题意: 求一个字符串的每个前缀在这个字符串中出现次数的加和 解析: 默默的骂一句...傻xkmp..博主心里气愤... 拓展kmp就好多了... 因为拓展kmp每匹配一次 就相当于这些前缀出现了一 ...
- Count the string - HDU 3336(next+dp)
题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", &quo ...
- Count the string -- HDOJ 3336
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优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- 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 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 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(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- ios 真机测试与发布详细流程,基于最新的开发者网站,ios7,xcode5(有截图的哦)[[[第一部分真机测试]]]
转载于:http://blog.csdn.net/lv_ruanruan/article/details/14446597 真机测试及发布详细流程,最新版 第一次一个人搞一个项目,我们老大规定,一个周 ...
- 多个jsp页面共享Java bean
通过jsp的内置对象—request对象获取参数: 通过超链接传参: 例:把a.jsp中i的值传到b.jsp中: 在a.jsp页面中的核心代码为: <a href="b.jsp?i=1 ...
- python中的__name__=='__main__'如何简单理解(一)
1. 摘要: 通俗的理解_name_ == '_main_':假如你叫小明.py,在朋友眼中,你是小明(_name_ == '小明'):在你自己眼中,你是你自己(_name_ == '_main_') ...
- Ansible test
[root@localmesos ansible_test]# ansible all -a "/bin/echo hello"192.168.111.111 | SUCCESS ...
- express中使用ejs
[express中使用ejs] 1.添加 ejs 依赖. npm install ejs --save 2.设置 view engine 为 ejs 即可.
- 通过jquery,从json中读取数据追加到html中
1.下载安装jquery 可通过下面的方法引入在线版本的js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jque ...
- 最短路径Dijkstra算法(邻接矩阵)
Dijkstra算法的原理: 从某个源点到其余各顶点的最短路径,即单源点最短路径(仅适合非负权值图).单源点最短路径是指:给定带权有向图G和源点v,求从v到G中其余各顶点的最短路径.迪杰斯特拉(Dij ...
- eclipse搭建struts2环境及所遇到的问题
最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错 首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为 ...
- ECMAScript5新特性之isSealed、seal
封闭对象后: 1 不能增加属性. 2 不能删除属性. 3 可以修改属性.(赋值) 4 不能修改属性描述符.(抛异常) var fruit = { name : '苹果', desc : '红富士' } ...
- mongo远程登录
1. 进入数据库: use admin db.addUser("foo","foo"); ps:高版本用db.createUser创建. 2. 改配置 如/et ...