ZCMU—1893

来源:互联网 发布:mysql局域网访问配置 编辑:程序博客网 时间:2024/06/01 08:26

1893: String Game

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

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

【分析】

题意比较简单,给两个字符串a,b,两个人轮流选择从头或者尾删除一个字符,B先手,问有没有某一步之后可能使得剩余字符串a与b相等,如果能,那么A赢否则B赢。
显然A赢的可能性比较少所以考虑A赢
最容易发现的就是如果b在a的正中间,那么肯定是A赢,不管B删除哪边,A删除另一边保持对称就可以了
第二种情况就是b不在a的中间,那么什么情况下A可以赢,肯定首先要满足对称,也就是说肯定会存在2个b在a中,在这个条件下,模拟试试看就知道了,就是对称并且中间距离小于2的时候,那么A只要跟着B拿,就可以保证b一直存在于a中
【代码】
#include <string.h>#include <stdio.h>char a[500100];char b[500100];int main(){    int pp;scanf("%d",&pp);    while (pp--)    {        scanf("%s%s",a,b);        int len1=strlen(a);        int len2=strlen(b);        int mid=len1-len2;mid/=2;        if (len1<len2)         {            printf("Bob\n");            goto out;        }        if ((len1-len2)%2==0)        {            int t=1;            for (int i=0;i<len2;i++)                if (a[mid+i]!=b[i])                {                    t=0;                    break;                }            if (t)            {                printf("Alice\n");                goto out;            }            t=1;            for (int i=0;i<len2;i++)                if (a[mid+i+1]!=b[i])                {                    t=0;                    break;                }            for (int i=0;i<len2;i++)                if (a[mid+i-1]!=b[i])                {                    t=0;                    break;                }            if (t)            {                printf("Alice\n");                goto out;            }            else            {                printf("Bob\n");                goto out;            }        }        else        {            int t=1;            for (int i=0;i<len2;i++)                if (a[mid+i]!=b[i])                {                    t=0;                    break;                }            for (int i=0;i<len2;i++)                if (a[mid+i+1]!=b[i])                {                    t=0;                    break;                }            if (t)            {                printf("Alice\n");                goto out;            }            else            {                printf("Bob\n");                goto out;            }        }        out:;    }    return 0;}


0 0
原创粉丝点击