Game
Time limit 1000 ms Memory limit262144 kBAlice and Bob is playing a game.
Each of them has a number. Alice’s number is A, and Bob’s number is B.
Each turn, one player can do one of the following actions on his own number:
1. Flip: Flip the number. Suppose X = 123456 and after flip, X = 654321
2. Divide. X = X/10. Attention all the numbers are integer. For example X=123456 , after this action X become 12345(but not 12345.6). 0/0=0.
Alice and Bob moves in turn, Alice moves first. Alice can only modify A, Bob can only modify B. If A=B after any player’s action, then Alice win. Otherwise the game keep going on!
Alice wants to win the game, but Bob will try his best to stop Alice.
Suppose Alice and Bob are clever enough, now Alice wants to know whether she can win the game in limited step or the game will never end.
InputFirst line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Two number A and B. 0<=A,B<=10^100000.
OutputFor each test case, if Alice can win the game, output “Alice”. Otherwise output “Bob”.
Sample Input 4 11111 1 1 11111 12345 54321 123 123 Sample Output Alice Bob Alice Alice HintFor the third sample, Alice flip his number and win the game.
For the last sample, A=B, so Alice win the game immediately even nobody take a move.
是一个KMP水题,但当时有组特殊样例没有考虑到,一直在wrong、。
那组样例就是:当后者的数为零的时候,前者一定赢。
当时没想到的原因是,我只考虑到了样例的一半,也就是我只是在想母串中会出现字串中不含有的字符,却没有考虑到字串如果含有母串中不含有的字符会怎样,一定是不成立吗?
AC代码:
#include <iostream> #include<algorithm> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<deque> #include<stack> using namespace std; char small[1000090],big[1000009],kkk[1000009]; int check[1000009]; int a,b; void preview() { int k=-1,j=0; check[0]=-1; while(j<a) { if(k==-1||small[k]==small[j]) check[++j]=++k; else k=check[k]; //对自己本身使用KMP思想,来优化代码,也可以用暴力的方法。 } } void preview1() { int k=-1,j=0; check[0]=-1; while(j<a) { if(k==-1||small[k]==small[j]) check[++j]=++k; else k=check[k]; //对自己本身使用KMP思想,来优化代码,也可以用暴力的方法。 } } int KMP_times() { int ans=0,k,i,j; j=0; for(i=0; i<b; i++) { while(j>0&&small[j]!=big[i]) j=check[j]; if(small[j]==big[i]) j++; if(j==a) { ans++; j=check[j]; } } return ans; } int main() { int T; scanf("%d",&T); while(T--) { int t,i,p,j=0,n; int x,y; x=0; scanf(" %s %s",big,small); /* if(strlen(small)>strlen(big)) { strcpy(kkk,big); strcpy(big,small); strcmp(small,kkk); }*/ a=strlen(small); b=strlen(big); if(a>b) printf("Bob\n"); else { if(small[0]=='0') { printf("Alice\n"); continue; } else { preview(); if(KMP_times()>0) x++; for(i=a-1; i>=0; i--) kkk[j++]=small[i]; kkk[j]=0; strcpy(small,kkk); preview1(); if(KMP_times()>0) x++; if(x>0) printf("Alice\n"); else printf("Bob\n"); } } } return 0; }