HDU 3336 KMP
题意:求每一个前缀,跟前缀相同的每个子串。
此题:网上很多都是假程序,不过也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的更多相关文章
- hdu 3336 kmp+next数组应用
分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...
- HDU 3336 (KMP next性质) Count the string
直接上传送门好了,我觉得他分析得非常透彻. http://972169909-qq-com.iteye.com/blog/1114968 #include <cstdio> #includ ...
- 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) ...
- hdu poj KMP简单题目总结
hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i] ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- 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 ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- (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 ...
随机推荐
- 2.5 References & Borrowing
Here is how you would define and use a calculate_length function that has a reference to an object a ...
- Knime读取Jason数据
Knime ETL 工具 Jason数据解析到DB 1. 下面例子是一段Jason代码 [{,,},{,,},{,,}] 2. 用文本文件存储上面代码. test_jason.txt 3. 用File ...
- java.lang.IllegalArgumentException: Result Maps collection already contains value for xxx
本人项目产生此问题的原因是: 本地备份了一份xxxmapper.xml的副本“xxxmapper - 副本.xml”,应该是系统会自动加载“mappe”目录下的所有xml文件. 参考:https:// ...
- android主线程ActivityThread
ActivityThread在Android中它就代表了Android的主线程,但是并不是一个Thread类. 源码如下: http://androidxref.com/6.0.0_r1/xref/f ...
- like模糊查询%注入问题
android like 全局模糊查找文件命名 通过条件通过 like %search% 如果查找的关键字是% 那么就成了 like %%% 就会查找出所有的文件 解决办法是先把正则里面的匹配符 替换 ...
- scala 中格式化字符常用的格式符
val name="Fred" val age=20 val weight=150.00 val dd="%s's age is %d,weighs %.2f" ...
- Android中的CardView使用
Android 5.0 版本中新增了CardView,CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得控件具有立体性,也可以包含其他的布局容器和控件. 1.配置build. ...
- URL篇之URL
URL(统一资源定位)是网络上使用的资源定位的方案,它是URI(由URL和URN组成)的子集. URL的通用格式 <scheme>://<user>:<password& ...
- winfrom 树勾选
树勾选 /// <summary> /// 树勾选 /// </summary> /// <param name="sender"></p ...
- spring+springmvc+mybatis 开发JAVA单体应用
myshop 概述 myshop项目是根据视频教程 Java 单体应用 做的一个练习项目,目前完成了登录功能.用户管理.类别管理后续有时间会继续做其它的功能.趁着双11花了99元一年买了台阿里云服务器 ...