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

此题:网上很多都是假程序,不过也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. 2.3 if switch for等流程控制

    if条件中可以写多个语句,语句的作用域仅限于if,不可在if之外的地方使用 package main import ( "fmt" "io/ioutil" ) ...

  2. 转 Monitoring Restore/Recovery Progress

    ora-279 是可以忽略的报错 In general, a restore should take approximately the same time as a backup, if not l ...

  3. oracle 备份恢复篇(五)---rman 剩下控制文件和spfile

    一,环境准备 ❤ 拥有全量备份文件

  4. (转)CentOS 7 sytemctl 自定义服务开机启动

    CentOS 7 sytemctl 自定义服务开机启动 原文:http://blog.csdn.net/ithomer/article/details/51766319 CentOS 7继承了RHEL ...

  5. Java中的时间处理

    日期时间组件使用 java.util.Date:实现类,其对象具有时间.日期组件.java.util.Calendar:抽象类,其对象具有时间.日期组件.java.sql.Date:实现类,其对象具有 ...

  6. C++ Memory System Part1: new和delete

    在深入探索自定义内存系统之前,我们需要了解一些基础的背景知识,这些知识点是我们接下来自定义内存系统的基础.所以第一部分,让我们来一起深入了解一下C++的new和delete家族,这其中有很多令人吃惊的 ...

  7. VS中为什么不同的项目类型属性查看和设置的界面不一样

    在VS中,存在ATL.MFC.Win32.CLR.常规等等各种工程模板,这些工程模板对应于开发不同类型的应用,比如要开发com,你应该选ATL:开发最原始的通过API代用操作系统的应用,应该用Win3 ...

  8. PHP Mongodb API参考

    <?php /*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-&g ...

  9. Python正则表达

    ```# -*- coding:utf-8 -*-import re re - Support for regular expressions (RE).正则表达式是一个特殊的字符序列,它能帮助你方便 ...

  10. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...