Alice and Bob are playing the following game:
a word is written on paper in front of the players
in one move, a player can erase one letter, or two identical letters, or, all letters of the same type
players take turns
Alice is the first to start
it is the player, who erases the last letter, that wins.
Write a program that will determine the winner of the described above game for a given word (given that the players play optimally (successfully).
Input
The input contains a single line — the starting word. The word consists of capital Latin letters, and its length does not exceed 40 characters.
Output
Type “Alice” (without quotes) if Alice wins, or “Bob” (without quotes) if Bob wins.
Examples
Input
ZADACHA
Output
Alice
Input
WORD
Output
Bob
Note
For example, in the word “ZADACHA”, Alice erases all the letters “A” by her first move. The word “Z.D.CH.” remains, and she wins because, in subsequent moves, the players are forced to erase only one letter at a time.
#include <bits/stdc++.h>
using namespace std;
int dp[50];
int a[26];
set<int> s;
string ch;
int main()
{
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= 40; i++)
{
s.clear();
s.insert(0);
if (i > 1) s.insert(dp[i - 1]);
if (i > 2) s.insert(dp[i - 2]);
int k = 0;
for (; s.find(k) != s.end(); k++);
dp[i] = k;
}
cin >> ch;
int n = ch.length();
for (int i = 0; i < n; i++)
{
a[ch[i] - 'A']++;
}
int flag = 0;
for (int i = 0; i < 26; i++)
{
flag ^= dp[a[i]];
}
if (flag)
puts("Alice");
else
puts("Bob");
}
本文介绍了一个Alice和Bob玩的字母消除游戏,玩家每次可以擦除一个、两个相同或所有同一类型的字母。Alice先手,最后擦除字母者获胜。通过示例和解释,展示了如何确定在最优情况下谁会赢得游戏。输入为起始单词,程序将判断Alice或Bob是否能获胜。例如,单词ZADACHA中,Alice首动消除所有'A',赢得游戏;而单词WORD中,无论谁先手,最后都会剩下单个字母,导致Bob获胜。
2873

被折叠的 条评论
为什么被折叠?



