Problem A: String Game
Time Limit: 1 Sec
Memory Limit: 128 MB
Submit: 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 <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <cstring>
#include <algorithm>
using
namespace
std;
int
main() {
int
n;
string str1, str2;
cin >> n;
while
(n--) {
cin >> str1 >> str2;
int
len1 = str1.length();
int
len2 = str2.length();
if
(len1 < len2) cout <<
"Bob"
<< endl;
else
{
int
t = (len1 - len2) / 2;
if
(len1 % 2 == len2 % 2) {
int
flag = 1;
for
(
int
i = 0; i < len2; i++) {
if
(str1[t + i] != str2[i]) {
flag = 0;
break
;
}
}
if
(flag) {
cout <<
"Alice"
<< endl;
}
else
if
(len1 == len2) {
cout <<
"Bob"
<< endl;
}
else
{
flag = 1;
for
(
int
i = 0; i < len2; i++) {
if
(str1[t + i + 1] != str2[i]) {
flag = 0;
break
;
}
}
for
(
int
i = 0; i < len2; i++) {
if
(str1[t + i - 1] != str2[i]) {
flag = 0;
break
;
}
}
if
(flag) cout <<
"Alice"
<< endl;
else
cout <<
"Bob"
<< endl;
}
}
else
{
int
flag = 1;
for
(
int
i = 0; i < len2; i++) {
if
(str2[i] != str1[t + i]) {
flag = 0;
break
;
}
}
for
(
int
i = 0; i < len2; i++) {
if
(str2[i] != str1[t + i + 1]) {
flag = 0;
break
;
}
}
if
(flag) cout <<
"Alice"
<< endl;
else
cout <<
"Bob"
<< endl;
}
}
}
return
0;
}