Pagini recente » Cod sursa (job #1691233) | Cod sursa (job #1267826) | Cod sursa (job #1841465) | Cod sursa (job #496841) | Cod sursa (job #1716282)
#include <cstdio>
#include <cstring>
using namespace std;
char a[2000005], b[2000005];
int p[2000005], sol[1001], m, n, q, total;
void buildPrefix()
{
int i = 2;
q = 0;
for(p[1] = 0; i <= m; ++i)
{
while(q && a[q + 1] != a[i]) q = p[q];
if(a[q + 1] == a[i]) ++q;
p[i] = q;
}
}
int main()
{
freopen("strmatch.in", "r", stdin);
freopen("strmatch.out", "w", stdout);
scanf("%s\n%s", a + 1, b + 1);
fclose(stdin);
m = strlen(a + 1); n = strlen(b + 1);
if(m > n)
{
printf("0");
return 0;
}
buildPrefix();
q = 0;
for(int i = 1; i <= n; ++i)
{
while(q && a[q + 1] != b[i]) q = p[q];
if(a[q + 1] == b[i]) ++q;
if(q == m)
{
q = p[m];
if(total < 1000) sol[total++] = i - m;
}
}
printf("%d\n", total);
for(int i = 0; i < (total < 1000 ? total : 1000); ++i) printf("%d ", sol[i]);
fclose(stdout);
return 0;
}