题意:求每一个前缀,跟前缀相同的每个子串。

此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题。

分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次呢?

如果f > 0 ,至少这个前缀是符合的,但是,你会少算一些,例如:

你会少算子串a,怎么弥补回来呢? 继续递归下去(其实不是递归啦),找这个子串,是否还可以找出子串和前缀相同。

完美解决了~~~

#include <bits/stdc++.h>

using namespace std;

const int maxn = ;
const int MOD = ;
char P[maxn];
int f[maxn];
int len; int main()
{
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--) {
cin>>len>>P; f[] = f[] = ;
for(int i = ; i < len; i++) {
int j = f[i];
while(j&&P[i]!=P[j]) j = f[j];
f[i+] = P[i]==P[j] ? j+ : ;
} int ans= len; for(int i = ; i <= len; i++)
{
int tmp = f[i];
while(tmp)
{
ans = (ans + ) % MOD;
tmp = f[tmp];
}
}
cout<<ans<<endl;
} return ;
}

HDU 3336 KMP的更多相关文章

  1. hdu 3336 kmp+next数组应用

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

  2. HDU 3336 (KMP next性质) Count the string

    直接上传送门好了,我觉得他分析得非常透彻. http://972169909-qq-com.iteye.com/blog/1114968 #include <cstdio> #includ ...

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

  4. hdu poj KMP简单题目总结

    hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i] ...

  5. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  6. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  7. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  8. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

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

随机推荐

  1. js 中的! 和 !! 的区别

    Js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,1.!可将变量转换成boolean类型,null.undefined和空字符串取反都为false,其 ...

  2. Google Authenticator(谷歌身份验证器)

    <!DOCTYPE html>Google Authenticator(谷歌身份验证器) ] Google Authenticator(谷歌身份验证器) Google Authentica ...

  3. python3 模块安装列表

    pip install scrapy pip install twisted pip install BeautifulSoup4 pip install lxml pip install Pillo ...

  4. Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错

    <!-- 添加oracle jdbc driver --> <dependency> <groupId>com.oracle</groupId> < ...

  5. vi和vim的基本介绍

    所有的 Linux 系统都会内建 vi 文本编辑器.   Vim 具有程序编辑的能力,可以看做是Vi的增强版本,可以主动的以字体颜色辨别语法的正确性,方便程序设计.代码补完.编译及错误跳转等方便编程的 ...

  6. Zabbix的安装(源码安装)

    zabbix3.0完全安装 安装zabbix首先需要安装ltmp(lnmp),这里的t是指tengine(taobao的nginx版本),安装教程见 http://www.ltmp.cc 安装的时候P ...

  7. unity接入讯飞教程

    [全流程]<按照这个流程做即可,有不懂得可以看下面的2个><这个是<eclipse>> http://blog.csdn.net/qq_15267341/artic ...

  8. unity摄像机移动滑动

    之前写了一个pc版本的 // 当按住鼠标左键的时候 //if (Input.GetMouseButton(0)) //{ // // 获取鼠标的x和y的值,乘以速度和Time.deltaTime是因为 ...

  9. java I/O流 温习随笔

    java I/O流的熟练掌握是十分重要的. 在我的理解中,I/O流可以分为两种:字符流.字节流.字符流就是可以用来传输字符的流,比如传输txt文本,简单的说,只有能被电脑中的记事本直接打开并且你能看懂 ...

  10. [转]v$parameter, v$parameter2, v$system_parameter, v$system_parameter2, v$spparameter区别

    本文转自:http://blog.csdn.net/huang_xw/article/details/617389 1 v$parameter v$parameter显示的是session级的参数. ...