4212: String Game

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 337  Solved: 41

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。从Bob开始,Bob和Alice两人轮流从a串左端或者右端删去一个字符,当跟b串长度相同时停止。如果a串这时是等于b串的,则Alice赢,否则Bob赢。问你最后谁赢。

解题思路:大家都能想到,如果a串中含有b串,且b串真好是位于a串的中间时,a串此时可以看做三部分,左部分,b串,右部分。这时肯定是Alice赢。Bob取一端,Alice取另一端。如:acfdebb fde。另外就是a串中含有两个b串,看作是平移2个位置。如:xyxyx xyx。这种也是Alice赢,因为Bob取一端,Alice跟他取同一端。还有就是出现平移一个位置的时候。如: acce c、 abccccba ccc。总体上是当两个串的奇偶性相同时作为一种情况,不同时作为另一种情况来处理。

(⊙o⊙) 代码写得乱得恶心。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
string A,B;
bool jud(int n,int m){
int pos = (n-m)/2;
for(int i = 0; i < m; i++){
if(A[i+pos] != B[i]){
return 0;
}
}
for(int i = 0; i < m; i++){
if(A[i+pos+1] != B[i]){
return 0;
}
}
return true;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
cin>>A>>B;
int n = A.size(), m = B.size();
if(n % 2 != m % 2){
printf("%s\n",jud(n,m) ? "Alice" : "Bob");
continue;
}
int mmid = m/2, nmid = n/2;
int flag1 = 1;
int i, j;
for( i = nmid, j = mmid; i >= 0 && j >= 0; i--,j--){
if(A[i] != B[j]){
flag1 = 0;
}
}
int cs = i+1;
for( i = nmid+1, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag1 = 0;
}
}
int ce = n - i;
int flag2 = 1;
for( i = nmid - 1, j = mmid; i >= 0 && j >= 0; j--,i--){
if(A[i] != B[j]){
flag2 = 0;
}
}
int ccs = i+1;
for( i = nmid, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag2 = 0;
}
}
for(i = nmid + 1, j = mmid; i >= 0 && j >= 0; j--,i--){
if(A[i] != B[j]){
flag2 = 0;
}
}
for(i = nmid + 2, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag2 = 0;
}
}
int cce = n - i;
if(cs != ce){
flag1 = 0;
}
if(ccs != cce){
flag2 = 0;
}
if(flag1 || flag2){
puts("Alice");
}else{
puts("Bob");
}
}
return 0;
} /* 55
acce c
abccccde ccc
xxx xx
xxc xx
cxxd xx
xyxyxyx xyxyx
aba b
bab b
aaab aab
xyz mnk
xyz xyz
*/

  

05-21 16:38