Pagini recente » Istoria paginii runda/haicasepoate/clasament | acsdfg | tema | Cod sursa (job #439457) | Cod sursa (job #2086413)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRINGLENGTH 2000002
#define PATTERNLENGTH 2000002
typedef char* string;
typedef size_t bool;
char text[STRINGLENGTH];
char pattern[PATTERNLENGTH];
size_t out[PATTERNLENGTH];
void precompute(string text, size_t strlens, size_t* out)
{
size_t j, i;
j = 0, i = 1;
while (i < strlens) {
if (text[j] - text[i])
{
if (j > 0)
{
j = out[j - 1];
continue;
}
else j--;
}
j++;
out[i] = j;
i++;
}
}
int main(int argc, char** argv)
{
freopen("strmatch.in", "r", stdin);
size_t strlens;
size_t strlenp;
int output[1000];
size_t i, j, t, k, match = 0, y = 0;
fgets(pattern, STRINGLENGTH, stdin);
strlenp = strlen(pattern);
if (pattern[strlenp - 1] == '\n') {
pattern[strlenp - 1] = '\0';
strlenp--;
}
fgets(text, STRINGLENGTH, stdin);
strlens = strlen(text);
if (text[strlens - 1] == '\n') {
text[strlens - 1] = '\0';
strlens--;
}
precompute(pattern, strlenp, out);
j = 0, match = 0;
for (i = 0; i <= strlens; i++) {
if (text[i] == pattern[j]) {
j++;
if (j == strlenp) {
if (y < 1000) {
output[y] = i - strlenp + 1;
y++;
}
match++;
j = out[j - 1];
}
} else {
if (j != 0) {
j = out[j - 1];
i--;
}
}
}
freopen("strmatch.out", "w", stdout);
printf("%d\n", match);
if (match > 1000) {
match = 1000;
}
for (t = 0; t < match; t++) {
printf("%d ", output[t]);
}
printf("\n");
fclose(stdin);
fclose(stdout);
return 0;
}