Pagini recente » Cod sursa (job #1250596) | Cod sursa (job #1909235) | Cod sursa (job #1268117) | Istoria paginii runda/antr8/clasament | Cod sursa (job #1474950)
#include <stdio.h>
#include <string.h>
char a[2000005], b[2000005];
int occ[1005], f[2000005], nOcc = 0, i, j, sizeP, sizeS;
void buildF()
{
f[0] = -1;
for (i = 1; i < sizeP; ++i)
{
j = f[i - 1];
while (j != -1 && a[i - 1] != a[j])
{
j = f[j];
}
f[i] = j + 1;
}
}
int main()
{
freopen("strmatch.in", "r", stdin);
freopen("strmatch.out", "w", stdout);
memset(a, 0, 2000005);
memset(b, 0, 2000005);
scanf("%s%s", a, b);
sizeP = strlen(a);
sizeS = strlen(b);
buildF();//build the bad suffix good prefix function
i = j = 0;
while (i < sizeS)
{
if (b[i] == a[j])//the characters match
{
if (j == sizeP - 1)//found an occurence
{
occ[nOcc++] = i-j;
if (nOcc == 1000)//max occurences found
{
break;
}
j = f[j];
}
else
{
i++;
j++;
}
}
else//use the bad suffix to find good prefix
{
if (j == 0)//no good prefix can exist, moving to the next char in string
{
i++;
}
else
{
j = f[j];
}
}
}
printf("%d\n", nOcc);
for (i = 0; i < nOcc; ++i)
{
printf("%d ", occ[i]);
}
}