Pagini recente » Cod sursa (job #2704601) | Cod sursa (job #467628) | Cod sursa (job #1379493) | Cod sursa (job #2498437) | Cod sursa (job #863623)
Cod sursa(job #863623)
#include <vector>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
#define LL long long
#define INFILE "strmatch.in"
#define OUTFILE "strmatch.out"
#define SMAX 200005
#define SOLMAX 1005
char a[SMAX], b[SMAX];
int t[SMAX], sol[SOLMAX];
int main() {
freopen("strmatch.in", "r", stdin);
freopen("strmatch.out", "w", stdout);
scanf("%s %s",a,b);
int res = 0;
t[0] = -1;
int k = 0, n = strlen(a), m = strlen(b);
for(int j=1; j<n; j++) {
while( k && a[j]!=a[k] ) k = t[k-1];
if( a[j]==a[k] ) k++;
t[j] = k;
}
k = 0;
for(int i=0; i<m; i++) {
while( k && b[i]!=a[k] ) k = t[k-1];
if( b[i]==a[k] ) k++;
if( k==n ) {
if( res<1000 ) sol[res] = i-n+1;
res++;
k = t[k-1];
}
t[i] = k;
}
printf("%d\n", res);
for(int i=0; i<min(res, 1000); i++)
printf("%d ", sol[i]);
return 0;
}