#include #include int suffix_is_prefix(const char * w, int total_length, int prefix_length) { assert(prefix_length < total_length); int i; for (i = 0; i < prefix_length; ++i) if (w[i] != w[i + (total_length - prefix_length)]) return 0; return 1; } int main(int argc, const char * argv[]) { int pos[1000]; int done = 0; int N = argc - 1; int c; if (N > 1000) { printf("%s: too many strings to look for.\n", argv[0]); return -1; } while (done < N && (c = getchar()) != EOF) { int i; for (i = 0; i < N; ++i) { const char * w = argv[i + 1]; if (pos[i] == -1) continue; if (w[pos[i]] == 0) { pos[i] = -1; ++done; continue; } while (c != w[pos[i]] && pos[i] > 0) { int j; for (j = pos[i] - 1; j > 0; --j) if (suffix_is_prefix(w, pos[i], j)) break; pos[i] = j; } if (c == w[pos[i]]) ++pos[i]; } } return (done == N) ? 0 : 1; }