FZUOJ-2275 Game

 Problem 2275 Game

Accept: 159    Submit: 539
Time Limit: 1000 mSec    Memory Limit : 262144 KB

 Problem 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

 Hint

For 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.

 Source

第八屆福建省大學生程序設計競賽-重現賽(感謝承辦方廈門理工學院)
博弈問題,有兩種操作,除10,或者反轉,只要任何一方操作後,得到的兩個數字相同,在有限步結束則Alice勝,否則Bob勝,
樣例1:Alice不斷除10,Bob只能除10或者反轉,總會和Alice出現相同的情況;
樣例2:Alice隨意操作,Bob只需反轉即可,而這不可能相同;
樣例3:Alice反轉即可;
樣例4:不用操作,Alice勝;
可以看出,如果Bob的序列長度大於Alice,Alice肯定輸,如果Bob的序列是Alice的子串,爲了避免和Alice一致,只能除10,最後到0時Alice勝,如果不是子串,Bob不斷反轉即可,這就變成了子串匹配問題,可以用KMP算法,對Bob正匹配一次,反轉匹配一次,要注意的是前導0,因爲這個WA了。。。
#include
#include
#include
#define _match(a,b) ((a)==(b))
using namespace std;
const int N = 100000 + 5;
typedef char elem_t;
char A[N],B[N],C[N];
int fail[N];

int pat_match(int ls,elem_t* str,int lp,elem_t* pat){
    int i,j;
    fail[0] = -1;
    for(j=1;j=0&&!_match(pat[i+1],pat[j]);i=fail[i]);
        fail[j] = (_match(pat[i+1],pat[j])?i+1:-1);
    }
    for(i=j=0;i=0){printf("Alice\n");continue;}
        int k=0;
        for(int i=lenb-1;i>=0;i--)
            C[k++] = B[i];
        k=0;
        while(C[k]=='0') {k++;lenb--;}
        if(pat_match(lena,A,lenb,C+k)>=0){printf("Alice\n");continue;}
        printf("Bob\n");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章