ACM - KMP题目小结 (更新中)
KMP算法题型大致有两类,一类是next数组的应用,一类是匹配问题。
next数组大多数是求字符串周期,或者是与前缀后缀有关,也可以应用在DP中。需要对next数组有一定理解才能做得出。
next数组有一些性质。L为字符串长度。
如,L%(L-next[L])==0,说明字符串S[0,L-next[L]]是重复子串。
周期串/求循环节:
HDU 1358 Period
http://acm.hdu.edu.cn/showproblem.php?pid=1358
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAXN 1000005
using namespace std;
int next[MAXN];
char str[MAXN];
void getFail()
{
; str[i]; ++i)
{
int j=next[i];
while(j&&str[i]!=str[j]) j=next[j];
next[i+]=(str[i]==str[j])?j+:;
}
}
int main()
{
;
while(scanf("%d",&n)&&n)
{
scanf("%s",str);
getFail();
printf("Test case #%d\n",++kase);
; i<=n; ++i)
{
int k=i-next[i];
&&i/k!=) printf("%d %d\n",i,i/k);
}
printf("\n");
}
;
}
HDU 3746 Cyclic Nacklace
http://acm.hdu.edu.cn/showproblem.php?pid=3746
POJ 2406 Power Strings
http://poj.org/problem?id=2406
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
];
];
void getFail()
{
; str[i]; ++i)
{
int j=next[i];
while(j&&str[i]!=str[j]) j=next[j];
next[i+]=(str[i]==str[j])?j+:;
}
}
int main()
{
]!='.')
{
getFail();
int L=strlen(str);
int p=L-next[L];
) printf("%d\n",L/p);
);
}
;
}
前缀/后缀:
POJ 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752
HDU 2594 Simpsons’ Hidden Talents
http://acm.hdu.edu.cn/showproblem.php?pid=2594
HDU 4300 Clairewd’s message
http://acm.hdu.edu.cn/showproblem.php?pid=4300
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<string>
#include<cstring>
using namespace std;
],lenb;
map<char,char> has;
string str,a,b,s;
//char has[30];
void getFail()
{
; i<str.size(); ++i)
{
int j=next[i];
while(j&&str[i]!=str[j]) j=next[j];
next[i+]=(str[i]==str[j])?j+:;
}
}
string ans;
void Find(int p)
{
while(!(next[p]<=(lenb-next[p]))) p=next[p];
ans.clear();
; i<a.size(); ++i) ans+=a[i];
for(int i=next[p]; i<lenb-next[p]; ++i) ans+=b[i];
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>s>>a;
b.clear();
has.clear();
; i<s.size(); ++i)
has[s[i]]=i+'a';
; i<a.size(); ++i)
b+=has[a[i]];
str=b+a;
lenb=b.size();
getFail();
Find(str.size());
cout<<ans<<endl;
}
;
}
其他:
HDU 3336 Count the string
http://acm.hdu.edu.cn/showproblem.php?pid=3336
字符串匹配问题主要有匹配单个模版串,寻找公共子串。难的是它的应用。
单串匹配:问一个字符串在另一个字符串里出现了多少次。
HDU 2087 剪花布条 (不能重叠)
http://acm.hdu.edu.cn/showproblem.php?pid=2087
POJ 3461 Oulipo (可以重叠)
http://poj.org/problem?id=3461
二维匹配:
POJ 2185 Milking Grid
http://poj.org/problem?id=2185
需要降维后再使用KMP。
公共子串:此类问题大多数是KMP+暴力枚举
POJ 3080 Blue Jeans
http://poj.org/problem?id=3080
POJ 3450 Corporate Identity
http://poj.org/problem?id=3450
其它:
待补充。。。
ACM - KMP题目小结 (更新中)的更多相关文章
- ACM -二分图题目小结
暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063 过 ...
- PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中……)
PTA|团体程序设计天梯赛-练习题目题解锦集(持续更新中) 实现语言:C/C++: 欢迎各位看官交流讨论.指导题解错误:或者分享更快的方法!! 题目链接:https://pintia.cn/ ...
- js坑爹笔试题目汇总(持续更新中)
把你的面试官问倒,你就是一个合格的面试者了,以下总结一些易错的js笔试题目,会持续更新中.欢迎关注 1,考察this var length = 10 function fn(){ alert(this ...
- Android开发面试经——4.常见Android进阶笔试题(更新中...)
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...
- 【小TIP】记录各种错误【更新中】
最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...
- 404boom 博客闪现【不断的优化更新中。。。】
404boom 博客闪现[不断的优化更新中...] 停止本篇博文EQ继续优化,所有博文将会在标签[cnblogs_v2 ]中重新整理,待完成统一放上链接 一:Java SE相关1.Java的概述2.J ...
- 史上最全的spark面试题——持续更新中
史上最全的spark面试题——持续更新中 2018年09月09日 16:34:10 为了九亿少女的期待 阅读数 13696更多 分类专栏: Spark 面试题 版权声明:本文为博主原创文章,遵循C ...
- 2020年腾讯实习生C++面试题&持续更新中(2)
2020年腾讯实习生C++面试题&持续更新中(2) hello,大家好~ 我是好好学习天天,天天编程的天天,一个每天都死磕技术,及时分享的技术宅~ 昨天分享的题目不知道大家是否看过了,以后我计 ...
- 2020年腾讯实习生C++面试题&持续更新中(1)
2020年腾讯实习生C++面试题&持续更新中(1) 腾讯面试整理(1) 最近大三的学生找实习生的同学非常多,给大家分享一篇腾讯实习生的面试题,关于面试题,会持续更新~~~ 也算是今天开通博客的 ...
随机推荐
- Eclipse远程调试出现“JDWP Transport dt_socket failed to initialize”的解决方案
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
- WCF技术剖析之二:再谈IIS与ASP.NET管道
原文地址:http://www.cnblogs.com/artech/archive/2009/06/20/1507165.html 在2007年9月份,我曾经写了三篇详细介绍IIS架构和ASP.NE ...
- 重启Apache报错apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName ... waiting的解决方法
启动apache提示 : apache2: Could not reliably determine the server's fully qualified domain name, using 1 ...
- TCP与UDP
TCP(Transmission Control Protocol,传输控制协议)是面向连接的协议:可靠.保证正确性:顺序到达:流量控制.拥塞控制:重传机制.窗口机制:对系统资源.时间要求多:流模式S ...
- ajax接触
1. function doSave() { ajax_get("${contextPath}/auth/functionsave", $("#editForm" ...
- IntelliJ IDEA 中properties中文显示问题
- MATLAB 损失函数画图
损失函数画图 Hinge loss function: \[H(z) = max(0,1-z)\] $\psi$-learning loss function: \[{\phi _s}(z) = \l ...
- SQL Server数据库(SQL Sever语言 函数以及SQL编程)
1.数学函数:操作一个数据,返回一个结果 --去上限: ceiling ☆select ceiling(price) from car --去下限:floor ☆select floor(price) ...
- lmdb简介——结合MVCC的B+树嵌入式数据库
lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...
- spring集成环境下的axis webservice的发布,调试
在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...