Pagini recente » Cod sursa (job #457600) | Cod sursa (job #98331) | Cod sursa (job #1204969) | Cod sursa (job #1298280) | Cod sursa (job #2225639)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 2000000
#define MAXPOS 1000
int n_matches, pattern_len, text_len;
int matches[MAXPOS];
char *pattern, *text;
int *pmt;
int main(void)
{
// allocate memory
pattern = malloc(sizeof(*pattern) * (MAXLEN + 1));
text = malloc(sizeof(*text) * (MAXLEN + 1));
pmt = malloc(sizeof(*pmt) * MAXLEN);
// read input
freopen("strmatch.in", "r", stdin);
fgets(pattern, MAXLEN, stdin);
fgets(text, MAXLEN, stdin);
pattern_len = strlen(pattern) - 1;
text_len = strlen(text) - 1;
// solve
// generate partial match table
pmt[0] = 0;
for (int i = 0, j = 1; j < text_len; j++) {
// try to use the last prefix
while (i > 0 && pattern[i] != pattern[j])
i = pmt[i - 1];
// if partial match, increment length of prefix
if (pattern[i] == pattern[j])
i++;
// save lenght of prefix
pmt[j] = i;
}
// find matches
for (int i = 0, j = 0; j < text_len; j++) {
// try to use the last prefix
while (i > 0 && pattern[i] != text[j])
i = pmt[i - 1];
// if partial match, increment length of prefix
if (pattern[i] == text[j])
i++;
// if match, save the index
if (i == pattern_len && n_matches < 1000) {
matches[n_matches] = j - pattern_len + 1;
n_matches++;
i = pmt[i - 1];
}
}
// write output
freopen("strmatch.out", "w", stdout);
printf("%d\n", n_matches);
for (int i = 0; i < n_matches; i++)
printf("%d ", matches[i]);
// free memory
free(pattern);
free(text);
free(pmt);
return 0;
}