HDU 3336 (KMP next性质) Count the string
直接上传送门好了,我觉得他分析得非常透彻。
http://972169909-qq-com.iteye.com/blog/1114968
#include <cstdio>
#include <cstring> const int maxn = + ;
const int MOD = ;
char s[maxn];
int next[maxn], l; void get_next()
{
int k = -, j = ;
next[] = -;
while(j < l)
{
if(k == - || s[k] == s[j])
{
k++;
j++;
next[j] = k;
}
else k = next[k];
}
} int main(void)
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &l);
scanf("%s", s);
get_next(); int ans = ;
for(int j = ; j < l; ++j)
if(next[j] > && next[j] + != next[j+])
ans = (ans + next[j]) % MOD;
ans = (ans + l + next[l]) % MOD;
printf("%d\n", ans);
} return ;
}
代码君
HDU 3336 (KMP next性质) Count the string的更多相关文章
- 【KMP+DP】Count the string
KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...
- hdu 3336 kmp+next数组应用
分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...
- HDU 3336 KMP
题意:求每一个前缀,跟前缀相同的每个子串. 此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题. 分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次 ...
- 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) ...
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- 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) ...
随机推荐
- CGI与Servlet的区别和联系
1. 定义: CGI(Common Gateway Interface 公共网关接口)是HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. 2. 功能: 绝大多 ...
- android 解决启动页面加载图片空白以及去掉标题栏
有时候启动页面根据白天晚上来启动时实现加载不同的图片效果,但是加载时会出现短暂的空白,解决方法如下: android:theme="@android:style/Theme.Transluc ...
- 修改tomcat 启动45秒
当我们需要增加Tomcat的启动时间,修改方法如下:
- C# 数据结构--排序[上]
概述 看了几天的排序内容,现在和大家分享一些常见的排序方法. 啥是排序? 个人理解的排序:通过对数组中的值进行对比,交换位置最终得到一个有序的数组.排序分为内存排序和外部排序.本次分享排序方法都为内存 ...
- [转载]Java学习这七年
从2005那会做自动化测试开始接触Java开始,至今近7年.今天正好项目结束,趁机整理下思路,确定后续方向. 前三个年头基本上集中于Java基础的学习,包括设计模式,从完全不懂,到看的懂但似乎又不懂, ...
- POJ 1731
#include<iostream> #include<string> #include<algorithm> using namespace std; int m ...
- maven_Error building POM (may not be this project's POM)错误
如果maven项目在执行编译等操作时报如题错误的话,请仔细检查pom.xml,一般是由pom的语法错误导致的,例如我的项目是因为: dependencies 元素下不应该有properties元素导致 ...
- ubuntu 13.10 64bit装BeyondCompare
1. Beyond Compare官网下载amd-64位的,安装失败,依赖于ia32-libs,但是这个文件已经不在源里了: 2. 官网下载tar.gz源码包,解压安装失败: 3. 直接装32位的,可 ...
- cojs 简单的最近公共祖先 解题报告
我曾经自己想过每考试一次就从考试题中找找idea来出题 这次又找到了一个,先不管原来的考试题是什么 考试题中其中的一部分就是今天的这道题目啦 当时考场上自己比较傻,没有注意到有用的性质,套用了之前黑白 ...
- lintcode:在二叉查找树中插入节点
题目: 在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树. 样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样 ...