东华大学2020年程序设计竞赛(同步赛)F.A Simple Game
题目描述
Alice and Bob play a game.Initially they have n binary strings (a string which consists of zeroes and ones). They take alternating turns, and Alice is moving first. During each turn, the player has to choose several (at least one) strings and do one of the following operations (you can perform different operations on different strings):
1:choose a character ‘1’, and replace it with ‘0’.
2:choose a contiguous substring consisting only of characters ‘0’, and replace it with ‘1’(The length of the substring is at least 2).
For example, if s = “11000”,then after operation 1 s can turn into “01000” or “10000”. if s = “11000”, then after operation 2 s can turn into “1110”, “1101” or “111”.
Whoever is unable to choose,loses.You have to determine who wins if they both play optimally.
输入描述:
The first line contains one integer t (1≤ t ≤100) — the number of test cases. Then the test cases follow.
For each test case, the first line contains one integern(1 ≤ n ≤ 10).
There arenlines following, the i-th of which contains a binary string of length not more than 1000.
输出描述:
For each test case print sdzNB if Alice can win and kgNB otherwise.
示例1
输入
2
2
00
1
3
11
11
11
输出
sdzNB
kgNB
简单博弈~
我们首先考虑只有一个字符串的博弈策略~
不难发现,对当前操作的那个人字符串如果只包含
0
0
0,那么必输,不理解的可以推理一下
00
00
00 或者
000
000
000 的情况,后面多个
0
0
0 的情况都可以通过前面递推 ,那么最优策略就很简单了,只要把
1
1
1 转化为
0
0
0 即可,尽可能让对面操作时仅剩下
0
0
0 就一定能赢~
知道最优博弈策略后就很简单了,对一个字符串只需判断其含字符
1
1
1 的个数即可,奇数
A
l
i
c
e
Alice
Alice 赢,反之则输~
那么推广到多个字符串怎么判断呢,我是构造了几个简单的例子发现其实和一个字符串答案一样,只要存在一个含奇数个
1
1
1 的字符串
A
l
i
c
e
Alice
Alice 一定能赢。我是这么理解的,
A
l
i
c
e
Alice
Alice 可以与
B
o
b
Bob
Bob 先把偶数字符串走掉,此时最后肯定是
A
l
i
c
e
Alice
Alice 先手,她只需要操作那个含奇数个
1
1
1 的字符串就能赢了~
AC代码如下:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int f(string s){
int cnt=0;
for(char c:s)
if(c=='1') cnt++;
return cnt%2;
}
int main(){
int t,n;
cin>>t;
while(t--){
string s;
cin>>n;
int num=0;
for(int i=0;i<n;i++) cin>>s,num+=f(s);
if(num) puts("sdzNB");
else puts("kgNB");
}
}