​传送门​​ 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 (which is initially a palindrome in this version) 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 is a palindrome and 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.

思路:

博弈,开始没看到给的是回文串wa了好几发。。。;
当0的个数为奇数并且大于1的时候,只要ALICE第一次将最中间的0变为1,就可以保证最后的结果是ALICE胜利。
当0的个数为偶数,无论ALICE变换哪一个0,BOB只要变换对应的0使其变为回文串就可以保证BOB一定胜利。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 998244353;
char s[1010];

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
scanf("%s",s+1);
int a = 0,b = 0;
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(s[i] == '0')
{
cnt++;
}
}
int flag = 0;
if(cnt % 2)
{
if(cnt > 1)
cout<<"ALICE"<<endl;
else
cout<<"BOB"<<endl;
}
else
{
cout<<"BOB"<<endl;
}
}
}