[CF755B] PolandBall and Game - 博弈论

[CF755B]

Description

两个人轮流说单词,每个人只能说自己的的单词库里的单词。被任意方说过的单词不能再说。一个单词只会在某个人的单词库中出现一次,但是可能同时出现在两个人的单词库中。最先没有单词说的人负。求游戏的胜负。 \(n,m \le 1000, L_s \le 500\)

Solution

最优策略必然是优先说掉所有公共单词,然后再说非公共单词。

设公共单词的数量为 \(c\)

\(c\) 为奇数,那么最后一个说出公共单词的是先手,此时若 \(n \ge m\) 则先手胜。

\(c\) 为偶数,那么最后一个说出公共单词的是后手,此时若 \(n > m\) 则先手胜。

Code
#include <bits/stdc++.h>
using namespace std;

map <string,int> mp;
string tmp;
int n,m;

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        cin>>tmp, mp[tmp]++;
    for(int i=1; i<=m; i++)
        cin>>tmp, mp[tmp]++;
    int c=0;
    for(map<string,int>::iterator it=mp.begin(); it!=mp.end(); it++)
        c+=(it->second==2);
    if(c&1)
        cout<<(n>=m?"YES":"NO")<<endl;
    else
        cout<<(n>m?"YES":"NO")<<endl;
}

猜你喜欢

转载自www.cnblogs.com/mollnn/p/11730267.html
今日推荐