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) ...
随机推荐
- spring mvc 学习笔记【1】---前言
扎扎实实地掌握好技术 ----------------------------------------------------------------------------------------- ...
- IntelliJ IDEA 比较当前版本文件与历史文件
前言: 写代码修改后怎样比较与历史文件的区别呢?idea提供了2种比较方式(目前笔者所了解到的) 一.SVN的版本比较 二.当前文件与历史版本比较
- 单例模式(.NET)
问题描述: 单例模式 Singleton Pattern 问题解决: (1)单例模式简介: Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个 ...
- 【POJ】【2420】A Star not a Tree?
模拟退火 Orz HZWER 这题的题意是在二维平面内找一点,使得这点到给定的n个点的距离和最小……0.0 模拟退火算法请戳这里 //POJ 2420 #include<ctime> #i ...
- Java多线程——<一>概述、定义任务
一.概述 为什么使用线程?从c开始,任何一门高级语言的默认执行顺序是“按照编写的代码的顺序执行”,日常开发过程中写的业务逻辑,但凡不涉及并发的,都是让一个任务顺序执行以确保得到想要的结果.但是,当你的 ...
- Codeforces Round #256 (Div. 2) Multiplication Table
C题, #include<cstdio> #include<cstring> #include<algorithm> #define maxn 5005 using ...
- Curl的毫秒超时的一个”Bug”
Curl的毫秒超时的一个”Bug” -- PHP老杨 最近我们的服务在升级php使用的libcurl, 期望新版本的libcurl支持毫秒级的超时, 从而可以更加精细的控制后端的接口超时, 从而提高整 ...
- Jedis 操作
http://www.cnblogs.com/liuling/p/2014-4-19-04.html
- C# MessageBox 用法大全(转)
C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...
- Thread的第一天学习
1.实现线程的方法: 1)extend Thread 2)implements Runnable 2.下面代码执行哪个run方法: new Thread( new Runnable(){ public ...