foj Problem 2275 Game

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一样的字符串,那么Alice赢,否则Bob赢

思路:仔细想想可以发现如果Bob的字符串如果是Alice的子串,那么Alice一定能通过有限的操作最终得到和Bob一样的字符串。

KMP字符串匹配即可,Alice的字符串以及其反串都判断一遍,注意在判断反串之前把反串中的前导0去掉。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int Next[1000000+5] = { 0 };
/* P为模式串,下标从0开始 */
void GetNext(string P, int next[])
{
    int p_len = P.size();
    int i = 0;   //P的下标
    int j = -1;
    next[0] = -1;

    while (i < p_len)
    {
        if (j == -1 || P[i] == P[j])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
}

/* 在S中找到P第一次出现的位置 */
int KMP(string S, string P, int next[]){
    GetNext(P, next);

    int i = 0;  //S的下标
    int j = 0;  //P的下标
    int s_len = S.size();
    int p_len = P.size();

    while (i < s_len && j < p_len)
    {
        if (j == -1 || S[i] == P[j])  //P的第一个字符不匹配或S[i] == P[j]
        {
            i++;
            j++;
        }
        else
            j = next[j];  //当前字符匹配失败,进行跳转
    }

    if (j == p_len)  //匹配成功
        return i - j;

    return -1;
}
string s1, s2;
int main(){
    int t;
    scanf("%d",&t);
    while (t--) {
        cin >> s1 >> s2;
        if (KMP(s1, s2, Next) != -1) { printf("Alice\n");  continue; }
        else {
            string tmp; bool flag = 0; int k;
            reverse(s2.begin(), s2.end());
            for (k = 0; k < s2.size();k++) //去掉前导0
                if (s2[k] != '0')break;
            tmp = s2.substr(k, s2.size());
            if (KMP(s1, tmp, Next) != -1)printf("Alice\n");
            else printf("Bob\n");
        }
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/ZefengYao/p/7259157.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值