直接上传送门好了,我觉得他分析得非常透彻。

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的更多相关文章

  1. 【KMP+DP】Count the string

    KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...

  2. hdu 3336 kmp+next数组应用

    分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...

  3. HDU 3336 KMP

    题意:求每一个前缀,跟前缀相同的每个子串. 此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题. 分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次 ...

  4. 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) ...

  5. (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 ...

  6. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  7. 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 ...

  8. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. ADT 怎么删除logcat过滤规则

    刚才新增了一个过滤规则,但是没有找到在哪里删除,也看不到全部的log输出, 解决方案如下:

  2. android开发 PopupWindow 设置充满屏幕

    View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...

  3. html+css学习笔记 2[标签]

    img标签/a标签 <img src="图片地址" alt="图片名"/>  图片(单标签)alt属性 是图片名字,是给百度搜索引擎抓取使用:    ...

  4. linux_fedora nexus_auto_start

      fedora20发布,不对rc.local支持,其实只是删除了rc.local文件,如果想在开机时能够运行自己写的脚本,只要新建rc.local文件就可以了,下面让我们来测试下吧: 环境:fedo ...

  5. Log4Net 日志配置[附带源码]

    前述 园子里有许多人对log4net这款开源的日志记录控件有很多介绍.在这里个人再做一次总结,希望对以后有所帮助,需要的时候可以直接使用,减少查阅资料的时间.利用log4net可以方便地将日志信息记录 ...

  6. display:none和visibility: hidden二三事

    display:none属性后,HTML元素(对象)的宽度.高度等各种属性值都将“丢失”;而使用visibility:hidden属性后,HTML元素(对象)仅仅是在视觉上看不见(完全透明),而它所占 ...

  7. 一个简单的PHP登录演示(SESSION版 与 COOKIE版)

    //==============COOKIE版本的简单登录================ if ($_GET[out]){ setcookie('id',''); setcookie('pw','' ...

  8. JS内存管理测试

    打开调试器,切换到timer,点击左下角的record按钮开始,切换到memory视图,在文档中点击鼠标左右键,看股价走势图 function Allocate(kbs){ this.mem = ne ...

  9. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  10. MSVC CRT运行库启动代码分析

    原文链接:http://www.programlife.net/msvc-crt-startup.html 在程序进入main/WinMain函数之前,需要先进行C运行库的初始化操作,通过在Visua ...