A word game (博弈sg函数)

本文介绍了一个Alice和Bob玩的字母消除游戏,玩家每次可以擦除一个、两个相同或所有同一类型的字母。Alice先手,最后擦除字母者获胜。通过示例和解释,展示了如何确定在最优情况下谁会赢得游戏。输入为起始单词,程序将判断Alice或Bob是否能获胜。例如,单词ZADACHA中,Alice首动消除所有'A',赢得游戏;而单词WORD中,无论谁先手,最后都会剩下单个字母,导致Bob获胜。

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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值