Description
Alice 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.
Input
First 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.
Output
For 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
题解:简单的查找字串。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <deque>
#define mem(a) memset(a,0,sizeof(a))
#define For(a,b) for(int a=0;a<b;a++)
using namespace std;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6+50;
char a[maxn];
char b[maxn];
char c[maxn];
int nexta[maxn];
void Find_Next(char s2[]){
nexta[0] = -1;
nexta[1] = 0;
int cnt = 0;
int leng = strlen(s2);
int i = 2;
while(i<=leng){
if(s2[i-1]==s2[cnt])
nexta[i++] = ++ cnt;
else if(cnt>0)
cnt = nexta[cnt];
else
nexta[i++] = 0;
}
}
int kmp(char s1[],char s2[],int x,int y){
int i,j;
i = j = 0;
if(x < y)
return -1;
Find_Next(s2);
while(i<x&&j<y){
if(j==-1||s1[i]==s2[j])
i++,j++;
else
j = nexta[j];
}
if(j==y)
return i-j+1;
else
return -1;
}
void Do(){
int cnt = 0;
for(int i=strlen(a)-1;i>=0;i--){
c[cnt++] = a[i];
}
c[cnt] = 0;
}
int main() {
int n,m;
bool res;
int t;
char buf;
int a1,a2;
int ans;
cin >> t ;
while(t--){
res = true;
cin >> a >> b;
int sa = strlen(a);
int sb = strlen(b);
mem(nexta);
ans = kmp(a,b,sa,sb);
if(ans == -1&&sa>=sb){
Do();
mem(nexta);
ans = kmp(c,b,sa,sb);
}
if(ans == -1)
res = false;
if(b[0]=='0')
res = true;
cout << (res?"Alice":"Bob") << endl;
}
return 0;
}