第八届福建省大学生程序设计竞赛 D.Game【思维+KMP】

来源:互联网 发布:java md5加密与解密 编辑:程序博客网 时间:2024/05/29 10:58

 Problem D Game

Accept: 145    Submit: 844
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.


题目大意:


Alice和Bob两个人,每人控制一个数字串,轮流操作,选择两种操作中的一个必须执行:


①将数字串翻转。645550000翻转之后是55546

②将数字串/10


Alice的目标是让两个数字串相等,Bob的目标是相反的,如果存在一种情况,使得Alice和Bob的数字串相等了,那么Alice获胜,否则Bob获胜。Alice先手


思路:


考虑两个操作的特性,无论怎样进行操作的顺序,我们都无法改变数字串中数字的相对位子,也就是说,Alice如果不存在一个子串能够包含Bob的数字串,那么无论Alice如何去操作,都一定不可能和Bob相等,比如:15454和14.我们是无法将Alice的字符串中间部分去掉的。


所以考虑Alice的数字串和Bob的数字串进行KMP匹配。


注意特殊情况,如果Bob的数字串是0.那么Alice一直/10操作即可,Alice必胜。

如果Bob的数字串中有后缀0.我们删除掉,因为只要进行了翻转后缀0就没有了。


而Bob为了防止Alice变成和Bob一样的数字串,肯定要原地拍换进行操作①的。

所以我们最终的思路就是:

①首先将Bob的数字串去掉后缀0.

②然后判断Bob的数字串是否为0.如果是输出Alice否则进入步骤③

③将Alice的数字串和Bob的数字串进行KMP匹配,正反各一次即可,如果包含了Bob的数字串,输出Alice,否则输出Bob.


Ac代码:

#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <queue>#define maxs 2020202#define mme(i,j) memset(i,j,sizeof(i))using namespace std;char s[1000005],s2[1000005];int nexts[maxs];void getn(){    int len=strlen(s2);    int i=0,j;    j=nexts[0]=-1;    for(i=0;i<len;)    {        if(j==-1 || s2[i]==s2[j])            nexts[++i]=++j;        else            j=nexts[j];    }}bool kmp(){    getn();    int len1=strlen(s),len2=strlen(s2);    int i=0,j=0;    for(i=0;i<len1;)    {        if(j==-1||s[i]==s2[j])        {            i++;            j++;        }        else            j=nexts[j];        if(j>=len2)            return 1;    }    if(j>=len2)        return 1;    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%s%s",s,s2);        int flag = 1;        for(int i=0;s2[i];i++){            if(s2[i]=='0') continue;            flag = 0 ;break;        }        int l2 = strlen(s2);        for(int i=l2-1;i>=0;i--){            if(s2[i]=='0')                s2[i]='\0';            else break;        }        if(flag||kmp())        {            printf("Alice\n");        }        else        {            reverse(s2,s2+strlen(s2));            if(kmp())            {                printf("Alice\n");            }            else printf("Bob\n");        }    }}













阅读全文
0 0
原创粉丝点击