Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(){
- char k1[100];
- char k2[100];
- scanf("%s", k1);
- scanf("%s", k2);
- int l1 = strlen(k1);
- int l2 = strlen(k2);
- int f1[100];
- int f2[100];
- int i = 0;
- int j = 1;
- f1[0] = 0;
- for(j; j < strlen(k1); j++){
- if(k1[i] == k1[j])
- i++;
- else
- i = 0;
- f1[j] = i;
- }
- int x = 0;
- int y = 0; //
- while(y < strlen(k2) && x < strlen(k1)){
- // printf("%d, %d, %c, %c\n", x, y, k1[x], k2[y]);
- if(k1[x] != k2[y]){
- if(x == 0){
- y++;
- }else{
- x = f1[x-1]; // k1 的第 f1[x-1] 個對齊 k2 的第 y 個字元
- }
- }else{
- x++;
- y++;
- }
- }
- printf("%d\n", y-x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement