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

此题:网上很多都是假程序,不过也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. jquery、js判断复选框是否选中

    js: if (document.getElementById("checkboxID").checked) { alert("checkobx is checked&q ...

  2. Eclipse/Myeclipse/Scala IDEA for Eclipse里两种添加插件的方法(在线和离线)

    不多说,直接上干货! 方法1:在线安装 第一步,在eclipse菜单栏下,选中help ---->Install New Software 第二步,点击图中 add 添加软件下载地址 第三步 , ...

  3. 015-GenericEncodingFilter模板【解决全局乱码】

    package ${enclosing_package}; import java.io.IOException; import java.io.UnsupportedEncodingExceptio ...

  4. tomcat修改域名和访问域名时去掉项目名

    打开tomcat安装目录,根据路径找到 server.xml   路径D:\apache-tomcat-7.0.70\conf\server.xml 打开后   找到这一段 <Connector ...

  5. Android模拟器访问本机服务器

    Android模拟器访问本机服务器,用127.0.0.1访问不到,因为127.0.0.1已经被映射到模拟器了. 可以用以下两种方式访问 1. 用 10.0.2.2 2. 直接用 本机的IP地址,如:1 ...

  6. flutter Failed to setup Skia Gr context导致白屏

    添加 --enable-software-rendering参数运行 G:\soft\flutter\project\hello_world> flutter run --enable-soft ...

  7. 使用 Azure CLI 创建虚拟机

    使用 az vm create 命令创建虚拟机. 创建虚拟机时,可使用多个选项,例如操作系统映像.磁盘大小调整和管理凭据. 在此示例中,创建了一个名为“myVM”的运行 Ubuntu Server 的 ...

  8. double类型计算

    下面两个例子体现两个运算规则 一.四舍五入 //四舍五入 double doublenum = Math.Round(12.5, MidpointRounding.AwayFromZero); //两 ...

  9. 纯代码编写的vc跳转SB

    今天遇到个问题,我整个项目都是纯代码,突然有个引用的VC用了storyboard,导航的跳转不知道如何操作,最后试了很多方法总算可以了 首先,找到要跳转的sb. UIStoryboard *story ...

  10. springboot如何实现微信登录,前期准备

    现在网站用微信登录真的是很多,那么具体是怎么实现的呢? 首先介绍的是微信开放平台,我们如果需要微信登录或者支付都需要在上面注册一个账号,用这个账号去为我们的网站申请的话,需要用到企业资料(家里有营业执 ...