CF1527B1 Palindrome Game (easy version)

题目描述
The only difference between the easy and hard versions is that the given string ss in the easy version is initially a palindrome, this condition is not always true for the hard version.

A palindrome is a string that reads the same left to right and right to left. For example, “101101” is a palindrome, while “0101” is not.

Alice and Bob are playing a game on a string ss (which is initially a palindrome in this version) of length nn consisting of the characters ‘0’ and ‘1’. Both players take alternate turns with Alice going first.

In each turn, the player can perform one of the following operations:

Choose any ii ( 1 \le i \le n1≤i≤n ), where s[i] =s[i]= ‘0’ and change s[i]s[i] to ‘1’. Pay 1 dollar.
Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can’t reverse in the next move, and vice versa.
Reversing a string means reordering its letters from the last to the first. For example, “01001” becomes “10010” after reversing.

The game ends when every character of string becomes ‘1’. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.

输入格式
The first line contains a single integer tt ( 1 ≤ t ≤ 1000 ). Then tt test cases follow.

The first line of each test case contains a single integer nn ( 1≤n≤1000).

The second line of each test case contains the string ss of length nn , consisting of the characters ‘0’ and ‘1’. It is guaranteed that the string ss is a palindrome and contains at least one ‘0’.

Note that there is no limit on the sum of nn over test cases.

输出格式
For each test case print a single word in a new line:

“ALICE”, if Alice will win the game,
“BOB”, if Bob will win the game,
“DRAW”, if the game ends in a draw.

题意翻译
给定一个01回文串s,可以进行如下操作

修改一个0为1,消耗一点能量

翻转这个串,条件是这个串当前是不回文的,且上一个人没有翻转

ALICEALICE和BOBBOB轮流操作,均采用最优策略,ALICEALICE先手,当这个串全部为11时花费能量少者获胜,输出获胜者名字,一行一个,平局输出DRAWDRAW

输入
多测,第一行测试组数t

接下来2t行,每2行为一组,包含回文串长度和回文串

输入

2
4
1001
1
0

输出

BOB
BOB

说明/提示

In the first test case of the example,
in the 1 -st move Alice has to perform the 1 -st operation, since the string is currently a palindrome.
in the 2 -nd move Bob reverses the string.
in the 3 -rd move Alice again has to perform the 1 -st operation. All characters of the string are '1', game over.
Alice spends 2 dollars while Bob spends 0 dollars. Hence, Bob always wins.

思路

//给一个回文串,ALICE先手,
//如果是偶数,BOB必胜:ALICE先,所以他不能翻转,只能修改,然后BOB再修改,使字符串又变成一个回文的,直到剩下两个0的时候,ALICE修改,只剩1个0,此时不是回文,然后BOB就翻转,不消耗能量,此时ALICE只能修改掉最后一个0,此时全部都是1,ALICE消耗的能量比BOB多2,所以偶数时BOB必胜 
//如果是奇数(1除外), ALICE必胜:ALICE操作一次使字符串变成回文,此时0为偶数,这样来看,就回到了上一步操作,只是先手变成了BOB,一番操作后,BOB的最终能量比ALICE多1,所以ALICE必胜 
//如果只有一个0,BOB必胜:不必多说
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,sum=0;
		string s;
		cin>>n>>s;
		for(int i=0;i<s.length();i++)
		{
			if(s[i]=='0')
			{
				sum++;
			}
		}
		if(sum==1)
		{
			cout<<"BOB"<<endl;
		}
		else if(sum%2==0)
		{
			cout<<"BOB"<<endl;
		}
		else
		{
			cout<<"ALICE"<<endl;
		}
	}
	return 0; 
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值