FZU2275 Game(kmp

 

暑假wa的题了,,,看见vj的attempt痕迹打算挨个补了,简单kmp题,判断bob的串是不是全为0或者是alice的字串就好了

 1 #include<algorithm>
 2 #include<iostream>
 3 using namespace std;
 4 const int N = 1e6+6;
 5 int nt[N];
 6 string tt,ss;
 7 bool KMP()
 8 {
 9     int s = ss.size(),t = tt.size();
10     if(s>t)return false;
11     nt[0] = -1;
12     int num = 0;
13     for(int i = 0,j = -1;i < s;){
14         if(j==-1||ss[i]==ss[j]){
15             i++;j++;nt[i]=j;
16         }
17         else j = nt[j];
18     } 
19     for(int i = 0,j=0;i<t;){
20         if(j==-1||ss[j]==tt[i]){
21             i++;j++;
22         }
23         else j = nt[j];
24         if(j==s){
25             return true;
26         }
27     }
28     return false;
29 }
30 int main()
31 {
32     ios::sync_with_stdio(0); 
33     int T;
34     cin>>T;
35     while(T--){
36         cin>>tt>>ss;
37         int ll = ss.size();
38         bool flag = 0;
39         for(int i = 0;i < ll;++i){
40             if(ss[0]!='0')flag = 1;
41         }
42         if(flag==0){
43             puts("Alice");continue;
44         }
45         for(int i = 0;i <=ll;++i)nt[i] = 0;
46         if(KMP()){
47             puts("Alice");continue;
48         }
49         reverse(ss.begin(),ss.end());
50         for(int i = 0;i <=ll;++i)nt[i] = 0;
51         if(KMP()){
52             puts("Alice");continue;
53         }
54         puts("Bob");
55         
56     }
57     return 0;
58 }

 

posted @ 2019-10-25 19:56  小草今天又在摸鱼吗  阅读(176)  评论(0编辑  收藏  举报