
KMP
文章平均质量分 70
Unin88
这个作者很懒,什么都没留下…
展开
-
KMP算法学习笔记
先有一个流程的感受(http://kb.cnblogs.com/page/176818/)此处的讲解特别易懂! "部分匹配值"就是"前缀"和"后缀"的最长的共有元素的长度。以"ABCDABD"为例, - "A"的前缀和后缀都为空集,共有元素的长度为0; - "AB"的前缀为[A],后缀为[B],共有元素的长度为0; - "ABC"的前缀为[A, AB],后缀为[BC, C]原创 2014-12-24 10:29:10 · 508 阅读 · 0 评论 -
UVa LA 3026 Period
题意 对于每个i,求一个最大整数K>1(如果K存在),使得S的前i个字符组成的前缀是某个字字符串重复K次得到,输出所有存在K的i和对应的K。 分析 这是一个KMP的周期问题,我们知道的是对于字符串来说KMP周期是i - f[i],那么本题就迎刃而解,注意K > 1,因此,f[i]为0不合法。 代码 #include #include #include #incl原创 2015-02-12 10:36:51 · 499 阅读 · 0 评论 -
poj 2406 Power Strings
求解字符串是否是周期串,如果是输出循环了几次。KMP周期问题,循环节是i - next[i]。 代码: #include #include #include #include using namespace std; const int MAX = 1000010; char s[MAX]; int n,f[MAX]; voi原创 2015-02-12 13:32:58 · 331 阅读 · 0 评论 -
poj 2752 Seek the Name, Seek the Fame
题意 在字符串S中寻找出所有字串,它即是这个字符串的前缀,也是这个字符串的后缀,输出它们的长度。 知识储备与解题思路 KMP中的next数组的含义,对于以i-1为结尾的子串,T[0, next[i]-1] 与 T[i-next[i], i-1]是相等的,且这两个相等的字符串是这个字串的前后缀,以样例为例分析以下。 字符串 a b a b c a b a b | a b原创 2015-02-12 12:15:06 · 386 阅读 · 0 评论 -
hdu 2203 亲和串
http://acm.hdu.edu.cn/showproblem.php?pid=2203 裸的字符串匹配问题 AC代码: #include #include #include #include using namespace std; int next[200010]; char s[200010],t[100010]; void getnext(){ int i原创 2014-12-30 21:12:38 · 240 阅读 · 0 评论 -
hdu 3746 Cyclic Nacklace
http://acm.hdu.edu.cn/showproblem.php?pid=3746 本题实际上就是求最少需要在结尾后面补几个字符才能凑成两个循环,即KMP的周期问题。 AC代码: #include #include #include #include using namespace std; const int MAX = 100010; char原创 2014-12-25 15:05:04 · 394 阅读 · 0 评论 -
hdu 3336 Count the string
http://acm.hdu.edu.cn/showproblem.php?pid=3336 根据TMP求next数组的原理我们发现: 给出任意数字i,它的next值为j(j>0),那么j-1代表str[0]~str[j-1]这个前缀再主串中出现一次。 由此轻松解决本题 AC代码: #include #include #include #include using na原创 2014-12-24 21:26:18 · 337 阅读 · 0 评论 -
hdu 2594 Simpsons’ Hidden Talents
http://acm.hdu.edu.cn/showproblem.php?pid=2594 给定两个字符串,找到第一个字符串的前缀是第二个字符串的后缀且最长。我们可以把第二个字符串连接到第一个后面,利用KMP求next数组的原理快速得到答案。 AC代码: #include #include #include #include using namespace std;原创 2014-12-25 16:33:18 · 345 阅读 · 0 评论 -
hdu 1358 Period
http://acm.hdu.edu.cn/showproblem.php?pid=1358 寻找循环性前缀,利用KMP周期性。 AC代码: #include #include #include #include using namespace std; const int MAX = 1000010; char str[MAX]; int next[M原创 2014-12-25 15:37:31 · 374 阅读 · 0 评论 -
hdu 2087 剪花布条 与 hdu 1686 Oulipo
http://acm.hdu.edu.cn/showproblem.php?pid=1686 http://acm.hdu.edu.cn/showproblem.php?pid=2087 对于aaaaa aaa来说: 1686的答案就是2 2087的答案就是1 对于代码实现也只是微微的改动 1686代码: #include #include #include #includ原创 2014-12-24 22:00:45 · 388 阅读 · 0 评论 -
D D - Om Nom and Necklace
官方题解(讲的非常详细) This task is to determine whether a string is in the form of ABABA... ABA for each prefixes of a given string S For a prefix P, let's split it into some blocks, just like P = SS原创 2015-04-09 10:13:25 · 659 阅读 · 0 评论