Pagini recente » Cod sursa (job #1699901) | tema | Profil ichspielendeutsch | Cod sursa (job #1043101) | Cod sursa (job #2086415)
#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];
static inline __attribute__((always_inline))
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)
{
if (freopen("strmatch.in", "r", stdin) == NULL) {
return -1;
}
size_t strlens;
size_t strlenp;
int output[1000];
size_t i, j, t, match = 0, y = 0;
if (fgets(pattern, STRINGLENGTH, stdin) == NULL) {
return -1;
}
strlenp = strlen(pattern);
if (pattern[strlenp - 1] == '\n') {
pattern[strlenp - 1] = '\0';
strlenp--;
}
if (fgets(text, STRINGLENGTH, stdin) == NULL) {
return -1;
}
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--;
}
}
}
if (freopen("strmatch.out", "w", stdout) == NULL) {
return -1;
}
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;
}