KMP
来源:互联网 发布:幼儿园手指算法 编辑:程序博客网 时间:2024/06/07 09:12
求子串
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char a[1020], b[1020];
int i, j, next[1020], n, m;
while(scanf("%s",a+1)!= EOF&&a[1]!='#')
{
scanf("%s", b+1);
m=strlen(a+1);
n=strlen(b+1);
next[0]=0;
j=0;i=1;
while(i<=n)//对模拟串b求next[]
{
if(j==0||b[i]==b[j])
{
j++;i++;
next[i]=j;
}
else j=next[j];
}
i=j=1;
int cout=0;
while(i<=m)//KMP
{
if(j==0||a[i]==b[j])
{
i++;j++;
}
else j=next[j];
if(j>n)
{
cout++;
}
printf("%d\n", cout);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char a[1020], b[1020];
int i, j, next[1020], n, m;
while(scanf("%s",a+1)!= EOF&&a[1]!='#')
{
scanf("%s", b+1);
m=strlen(a+1);
n=strlen(b+1);
next[0]=0;
j=0;i=1;
while(i<=n)//对模拟串b求next[]
{
if(j==0||b[i]==b[j])
{
j++;i++;
next[i]=j;
}
else j=next[j];
}
i=j=1;
int cout=0;
while(i<=m)//KMP
{
if(j==0||a[i]==b[j])
{
i++;j++;
}
else j=next[j];
if(j>n)
{
cout++;
j=next[j];//重复子串
// j=1;//不重复子串
}}
printf("%d\n", cout);
}
return 0;
}
0 0
- KMP
- KMP
- KMP
- KMP
- KMP
- KMP
- kmp
- kmp
- KMP
- kmp
- KMP
- KMP
- KMP
- kmp
- KMP
- KMP
- kmp
- KMP
- Java应用程序如何使用memcached
- c#对第三方缓存redis的操作(-)
- ARM中的RO、RW和ZI DATA
- HDU 2037----今年暑假不AC
- Android_AndroidStudio运行时出现 finished with non-zero exit value 2
- KMP
- Java中控件的使用注意事项
- BZOJ 4033: [HAOI2015]T1
- week13---5月28日
- 《现代工业经济和信息化》学术刊约稿函
- LUA 调用 C 时候获取参数个数
- [leetcode][array] Longest Consecutive Sequence
- c#对第三方缓存redis的操作(二)
- Fp-Growth 代码