B2. Palindrome Game (hard version)

B2. Palindrome Game (hard version)

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
The only difference between the easy and hard versions is that the given string s 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 s of length n 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 i (1≤i≤n), where s[i]= ‘0’ and change 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.

Input
The first line contains a single integer t (1≤t≤103). Then t test cases follow.

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

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

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

Output
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.
Example
input

3
3
110
2
00
4
1010

output

ALICE
BOB
ALICE

Note
In the first test case of example,

in the 1-st move, Alice will use the 2-nd operation to reverse the string, since doing the 1-st operation will result in her loss anyway. This also forces Bob to use the 1-st operation.
in the 2-nd move, Bob has to perform the 1-st operation, since the 2-nd operation cannot be performed twice in a row. All characters of the string are ‘1’, game over.
Alice spends 0 dollars while Bob spends 1 dollar. Hence, Alice wins.
In the second test case of 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 wins.

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <algorithm>
using namespace std;



int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		string s;
		cin >> s;
		int s0 = 0;
		int a0 = 0;
		int hunwen = 1;
		for (int i = 0; i < s.size();i++) {
			if (s[i] != s[s.size()-1 - i]&&hunwen==1) {
				hunwen = 0;
			}
			if (s[i] == '0') {
				s0++;
			}
			if (s[i] == '0' && s[s.size() - 1 - i] == '1') {
				a0++;
			}
		}
		if (s[n  / 2] == '0'&&n%2!=0) {
			a0++;
		}
		if (hunwen == 1) {
		    if (n % 2 == 0) {
			     cout << "BOB" << endl;
		    }
		    else {
			     if (s0 % 2 == 0||s0==1) {
				    cout << "BOB" << endl;
			     }
			     else {
				    cout << "ALICE" << endl;
			     }
		    }
		}
		else {
			if (n % 2 == 0) {
				cout << "ALICE" << endl;
			}
			else {
				if (s0 == 2&& s[n / 2] == '0') {
					cout << "DRAW" << endl;
				}
				else {
					cout << "ALICE" << endl;
				}
			}
		}
	}
}

题意:除了处理的字符串为可能为非回文外,与B1相同
思路:考虑非回文的情况中,只有n为奇数且中间的数为0且0的总个数为2时平局,其他全为ALICE获胜;
回文情况与B1相同

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值