zcmu 1893: String Game
http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1893
1893: String Game
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 55 Solved: 20
[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
Output
Sample Input
Sample Output
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define maxn 500055
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char a[maxn],b[maxn];
scanf("%s%s",a,b);
int an=strlen(a),bn=strlen(b),flag=0;
int d=(an-bn)/2;
if((an-bn)%2)
{
if(strncmp(a+d+1,b,bn)==0&&strncmp(a+d,b,bn)==0)
flag=1;
}
else
{
if(strncmp(a+d,b,bn)==0)
flag=1;
else{
if(strncmp(a+d+1,b,bn)==0&&strncmp(a+d-1,b,bn)==0)
{flag=1;}
}
}
if(flag)
printf("Alice\n");
else
printf("Bob\n");
}
return 0;
}