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

本文介绍了一道关于游戏策略的博弈题,通过分析游戏规则,给出了一种有效的解题思路。利用KMP算法来判断玩家Alice是否有能力使她的数字串与玩家Bob的数字串相同,从而赢得游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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");

        }
    }
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值