Pagini recente » Cod sursa (job #1669112) | Cod sursa (job #731746) | Cod sursa (job #1522792) | Cod sursa (job #105538) | Cod sursa (job #1648945)
#include <fstream>
#include <cstring>
using namespace std;
ifstream x ("kmp.in");
ofstream y ("kmp.out");
int n,m;
int table[2000000];
char text[2000000];
char word[2000000];
void create_table()
{
int i;
int cnd=0;
table[0]=-1;
table[1]=0;
for(i=2;i<m;i++)
if(word[i-1]==word[cnd])
{
table[i]=cnd+1;
cnd+=1;
}
else
if(cnd>0)
cnd=table[cnd];
else
table[i]=0;
}
void kmp(int i)
{
int j=0;
while(i+m<=n)
if(word[j]==text[i+j])
{
if(j==m-1)
{
y<<i<<' ';
kmp(i+1);
break;
}
j++;
}
else
if(table[j]>-1)
{
i=i+j-table[j];
j=table[j];
}
else
{
i++;
//j=0;
}
}
int main()
{
int i;
x>>word;
x>>text;
n=strlen(text);
m=strlen(word);
create_table();
/*
for(i=0;i<m;i++)
y<<table[i]<<' ';
y<<'\n';
*/
kmp(0);
return 0;
}