Pagini recente » Cod sursa (job #2628808) | Cod sursa (job #2042250) | Cod sursa (job #1379822) | Cod sursa (job #409108) | Cod sursa (job #763320)
Cod sursa(job #763320)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BASE 73
#define MODULO 100007
char pattern[2000001], search[2000001];
int patternHash, searchHash, basePow, N, pos[1000];
int main()
{
FILE *in = fopen("strmatch.in", "r");
FILE *out = fopen("strmatch.out", "w");
int patternLength, searchLength, sw, i, j, temp;
fscanf(in, "%s\n%s", pattern, search);
patternLength = strlen(pattern);
searchLength = strlen(search);
basePow = 1;
patternHash = searchHash = 0;
for(i = 0; i < patternLength; i++)
{
patternHash = (patternHash * BASE + pattern[i]) % MODULO;
searchHash = (searchHash * BASE + search[i]) % MODULO;
if(i) basePow = (basePow * BASE) % MODULO;
}
if(patternHash == searchHash)
{
sw = 1;
for(i = 0; i < patternLength; i++)
if(search[i] != pattern[i])
sw = 0;
if(sw)
{
N++;
pos[0] = 0;
}
}
for(i = patternLength; i < searchLength; i++)
{
searchHash = (BASE * ((searchHash - basePow * search[i-patternLength]) % MODULO + MODULO)
+ search[i]) % MODULO;
if(patternHash == searchHash)
{
sw = 1;
for(j = 0 ;j < patternLength; j++)
if(search[i - patternLength + j + 1] != pattern[j])
sw = 0;
if(sw)
{
if(N < 1000) pos[N] = i - patternLength + 1;
N++;
}
}
}
temp = (N < 1000 ? N : 1000);
fprintf(out, "%d\n", N);
for(i = 0; i < temp; i++)
fprintf(out, "%d ", pos[i]);
fprintf(out, "\n");
fclose(in);
fclose(out);
return 0;
}