Pagini recente » Cod sursa (job #1976906) | Cod sursa (job #1118095) | Cod sursa (job #1116620) | Cod sursa (job #1990014) | Cod sursa (job #3128614)
#include <iostream>
#include <stdio.h>
#define LMAX 2000000
#define BASE 256
#define HASHSIZE1 666031
#define HASHSIZE2 4999999
using namespace std;
char pattern[LMAX+1];
int plength;
char str[LMAX+1];
int strlength;
int ap[1001];
int main() {
FILE *fin, *fout;
int i, noap;
long long pow1, pow2, h1, h2, ph1, ph2;
fin = fopen( "strmatch.in", "r" );
fout = fopen( "strmatch.out", "w" );
plength = 0;
pattern[0] = fgetc( fin );
while( pattern[plength] != '\n' )
pattern[++plength] = fgetc( fin );
pattern[plength] = 0;
strlength = 0;
str[0] = fgetc( fin );
while( str[strlength] != '\n' && str[strlength] != EOF )
str[++strlength] = fgetc( fin );
str[strlength] = 0;
h1 = h2 = ph1 = ph2 = 0;
pow1 = pow2 = 1;
for( i = 0; i < plength; i++ ) {
h1 = ( h1 * BASE + str[i] ) % HASHSIZE1;
h2 = ( h2 * BASE + str[i] ) % HASHSIZE2;
ph1 = ( ph1 * BASE + pattern[i] ) % HASHSIZE1;
ph2 = ( ph2 * BASE + pattern[i] ) % HASHSIZE2;
if( i > 0 ) {
pow1 = ( pow1 * BASE ) % HASHSIZE1;
pow2 = ( pow2 * BASE ) % HASHSIZE2;
}
}
noap = 0;
while( i <= strlength ) {
if( h1 == ph1 && h2 == ph2 ) {
noap++;
if( noap <= 1000 )
ap[noap] = i - plength;
}
h1 -= ( pow1 * str[i-plength] ) % HASHSIZE1;
h2 -= ( pow2 * str[i-plength] ) % HASHSIZE2;
if( h1 < 0 )
h1 += HASHSIZE1;
if( h2 < 0 )
h2 += HASHSIZE2;
h1 = ( h1 * BASE + str[i] ) % HASHSIZE1;
h2 = ( h2 * BASE + str[i] ) % HASHSIZE2;
i++;
}
fprintf( fout, "%d\n", noap );
for( i = 1; i <= min( 1000, noap ); i++ )
fprintf( fout, "%d ", ap[i] );
fclose( fin );
fclose( fout );
return 0;
}