Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void prefix(char *buf){
- int size = strlen(buf);
- int j;
- int pi[size-1];
- for (int i = 0;i < size-1; i++){
- pi[i] = 0;
- }
- for (int i = 1;i < size-1; i++){
- j = pi[i-1];
- while ((j > 0) && (buf[i] != buf[j])){
- j = pi[j-1];
- }
- if (buf[i] == buf[j]){
- pi[i] = j + 1;
- }
- else{
- pi[i] = j;
- }
- }
- printf("%d ", pi[size - 2]);
- }
- int main(void){
- char buf1[1000002], buf2[1000002];
- fgets(buf1, 1000002, stdin);
- fgets(buf2, 1000002, stdin);
- int size1 = strlen(buf1);
- int size2 = strlen(buf2);
- int size = size1 + size2 - 1;
- char buf[size];
- for (int i = 0; i < size; i++){
- buf[i] = 0;
- }
- strcpy(buf, buf1);
- buf[size1 - 1] = '#';
- strcat(buf, buf2);
- prefix(buf);
- for (int i = 0; i < size; i++){
- buf[i] = 0;
- }
- strcpy(buf, buf2);
- buf[size2 - 1] = '#';
- strcat(buf, buf1);
- prefix(buf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement