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<iostream>
#include<cstring>
#include<cstdio>
#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<lp;j++){
        for(i=fail[j-1];i>=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<ls && j<lp ;i++)
        if(_match(str[i],pat[j]))
            j++;
        else if(j)
            j=fail[j-1]+1,i--;
    return j==lp?(i-lp):-1;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",A);
        scanf("%s",B);
        int lena=strlen(A);
        int lenb=strlen(B);
        if(lena < lenb){printf("Bob\n"); continue;}
        if(B[lenb-1]=='0') {printf("Alice\n"); continue;}
        if(pat_match(lena,A,lenb,B)>=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");
    }
}

 


 

posted @ 2017-08-10 16:52  Pretty9  阅读(376)  评论(0编辑  收藏  举报