4212: String Game
Time Limit: 1 Sec Memory Limit:128 MBSubmit: 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;
}