FZU - 2275 (KMP)

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.


1A的一道KMP。虽然很简单,但也激动死我了。。哈哈哈。

思路:

主要是有一个特判,当B串,只有一个,并且是0的时候,一定是Alice 赢。其他就是利用KMP,来进行判断输出了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[110000],b[110000],c[110000];
int cnt;
int f[110000];
void find(char *T,char *P,int *f)
{
    int n=strlen(T);
    int m=strlen(P);
    int j=0;
    for(int i=0;i<n;i++)
    {
        if(j && T[i]!=P[j]) j=f[j];
        if(T[i]==P[j]) j++;
        if(j==m) cnt++;
    }
}
void getFail(char *P,int *f)
{
    int m =strlen(P);
    f[0]=f[1]=0;
    for(int i=1;i<m;i++)
    {
        int j=f[i];
        while(j && P[i]!=P[j]) j=f[j];
        f[i+1] = P[i]==P[j]?j+1:0;
    }
}
int work(char *q,char *w,int *e)
{
    cnt=0;
    getFail(w,e);
    find(q,w,e);
    return cnt;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        scanf("%s%s",a,b);
        int la=strlen(a);
        int lb=strlen(b);
        if(lb==1&&b[0]=='0')printf("Alice\n");
        else
        {
        if(la<lb)
        {
            printf("Bob\n");
        }
        else
        {
            for(int i=0;i<lb;i++)
            {
                c[i]=b[lb-i-1];
            }
            //cout<<c<<endl;
            int ans1=work(a,b,f);
            memset(f,0,sizeof(f));
            int ans2=work(a,c,f);
            if(ans1||ans2)
            {
                printf("Alice\n");
            }
            else printf("Bob\n");
        }
        }
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/a1046765624/article/details/80112509
kmp
今日推荐