Pku acm 2752 Seek the Name, Seek the Fame数据结构题目解题报告(二十)----kmp算法
2009-07-08 14:58
459 查看
关于kmp算法,大家都非常熟悉,其涵义非常难懂,以至于我学习kmp算法的时候一直怀疑自己的智商,其具体思想,这里就不多说了,网上也有很多资料,我用的是严蔚敏老师的书,实现也是按说上的方法实现的,推荐大家把kmp算法彻底弄明白了再做poj相关的几道题:2406 Power Strings 1961 Period 2752 Seek the Name, Seek the Fame。
该题的题意是这样的,给若干个字符串,判断该字符串前n个字符和后n个字符是否相同,按从小到大的顺序输出n,比如,给ababab,结果输出2 4 6,再比如ababcababababcabab 输出 2 4 9 18.
我们要根据kmp算法中的next函数来解决这个问题,以ababab为例加以说明:
String:ababab
Next: 0112345
这里根据后面的需要多计算了一位next值。
我们用ababab即作为主串有作为模式串来进行匹配,假设匹配到第7位时不匹配了(下标中1开始),要根据next[7](=5)的值继续匹配:
ababab*
ababab&
ababab*
ababab
可以由5得出4(5-1)是符合要求的,然后求next[5]=3,即继续这样匹配:
ababab*
ababab
可以由5得出2(3-1)是符合要求的,然后求next[3]=1,停止循环。
这样,我们依此输出2 4 6即可。代码如下:
j=length+1;
index=0;
while(j!=1)
{
result[++index]=j;
j=next[j];
}
for(i=index;i>0;i--)
printf("%d ",result[i]-1);
因为这里的next下标从1开始,所以造成了对下标操作的复杂。
带有详细注释的代码可以在http://download.csdn.net/user/china8848/获得
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/china8848/archive/2008/04/29/2343103.aspx
该题的题意是这样的,给若干个字符串,判断该字符串前n个字符和后n个字符是否相同,按从小到大的顺序输出n,比如,给ababab,结果输出2 4 6,再比如ababcababababcabab 输出 2 4 9 18.
我们要根据kmp算法中的next函数来解决这个问题,以ababab为例加以说明:
String:ababab
Next: 0112345
这里根据后面的需要多计算了一位next值。
我们用ababab即作为主串有作为模式串来进行匹配,假设匹配到第7位时不匹配了(下标中1开始),要根据next[7](=5)的值继续匹配:
ababab*
ababab&
ababab*
ababab
可以由5得出4(5-1)是符合要求的,然后求next[5]=3,即继续这样匹配:
ababab*
ababab
可以由5得出2(3-1)是符合要求的,然后求next[3]=1,停止循环。
这样,我们依此输出2 4 6即可。代码如下:
j=length+1;
index=0;
while(j!=1)
{
result[++index]=j;
j=next[j];
}
for(i=index;i>0;i--)
printf("%d ",result[i]-1);
因为这里的next下标从1开始,所以造成了对下标操作的复杂。
带有详细注释的代码可以在http://download.csdn.net/user/china8848/获得
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/china8848/archive/2008/04/29/2343103.aspx
相关文章推荐
- Pku acm 2752 Seek the Name, Seek the Fame数据结构题目解题报告(二十)----kmp算法
- POJ2752 Seek the Name, Seek the Fame 解题报告【字符串】【KMP】
- POJ2752 Seek the Name,Seek the Fame KMP算法
- poj 2752 Seek the Name, Seek the Fame(数据结构:KMP)
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
- POJ 2752 Seek the Name, Seek the Fame kmp算法
- POJ2752 Seek the Name, Seek the Fame KMP算法
- POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木
- POJ 题目2752 Seek the Name, Seek the Fame(KMP)
- 【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ - 2752
- POJ2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame(KMP的next数组)
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
- POJ2752 - Seek the Name, Seek the Fame(KMP)
- poj 2752 Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame(扩展KMP)
- POJ - 2752 Seek the Name, Seek the Fame —— next数组求循环节