ZSTUOJ 4212 String Game

4212: String Game

Time Limit: 1 Sec   Memory Limit:128 MB
Submit: 317   Solved:32

Description

Alice and Bob are playing the following game with strings of letters.

Before the game begins, an initial string and a target string are decided. The initial string is at least as long as the target string. Then, Alice and Bob take turns, starting with the initial string. Bob goes first. In each turn, the current player removes either the first or the last letter of the current string. Once the length of the current string becomes equal to the length of the target string, the game stops. If the string at the end of the game is equal to the target string, Alice wins the game; otherwise Bob wins.

Determine who will win the game if both players are playing optimally.

Input

Each test case starts with N, the number of inputs to process. Each input consists of one line, which contains the initial string, followed by a space, followed by the target string. Each string consists of only lowercase letters. The total input length will be less than 500000 characters.

Output

For each input, output the winner, which will either be Alice or Bob.

Sample Input

5
aba b
bab b
aaab aab
xyz mnk
xyz xyz

Sample Output

Alice
Alice
Bob
Bob
Alice

题意:给你两个串s,a,Bob和Alice可以将a串任意从前段或后段拿走一个字符,当a串的长度与b串相等时,如果a串等于b串,则Alice赢,否则Bob赢。
方法:判断a串是否在b串的中间,或者是存在另外一种情况abababa  ababa  若符合,则Alice,否Bob 如果a串中所有的元素与b串中相等,也是符合的 eg: aaa a

#include <bits/stdc++.h>

using namespace std;
const int maxn = 500010;
char s[maxn],a[maxn];

bool Judge(int len1, int len2)
{
    if(len1 >= len2)
    {
        int pos = (len1 - len2) >> 1;
        bool flag = true;
        if(len1 % 2 == len2 % 2)
        {
            for(int i=0; i<len2; i++)
            {
                if(a[i] != s[i+pos])
                {
                    flag = false;
                    break;
                }
            }
            if(flag) return true;
            if(len1 - len2 > 1)
            {
                flag = true;
                for(int i=0; i<len2; i++)
                {
                    if(a[i] != s[i+pos+1])
                    {
                        flag = false;
                        break;
                    }
                }
                for(int i=0; i<len2; i++)
                {
                    if(a[i] != s[i+pos-1])
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag) return true;
            }
        }
        else
        {
            for(int i=0; i<len2; i++)
            {
                if(a[i] != s[i+pos])
                {
                    flag = false;
                    break;
                }
            }
            for(int i=0; i<len2; i++)
            {
                if(a[i] != s[i+pos+1])
                {
                    flag = false;
                    break;
                }
            }
            if(flag) return true;
        }
    }
    return false;
}

int main()
{
    int T;
    cin>>T;
    while(T-- && cin>>s>>a)
    {
        int len1 = strlen(s);
        int len2 = strlen(a);
        if(Judge(len1,len2))
            cout<<"Alice"<<endl;
        else
            cout<<"Bob"<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值