首页 > 其他分享 >Codeforces Round #721 (Div. 2) B2. Palindrome Game (hard version)

Codeforces Round #721 (Div. 2) B2. Palindrome Game (hard version)

时间:2022-11-07 13:31:09浏览次数:92  
标签:cnt Palindrome string int hard Codeforces ALICE BOB 回文


​传送门​​ 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.

思路:

先找到字符串s中0的个数,记为cnt,再找到使该字符串变为回文串的最少改变次数,记为b,然后cnt = cnt-b;

当cnt为奇数时,并且s不为回文串,则易得BOB的最优决策为尽快使s变为回文串,这样他才有与ALICE周旋的可能(即在s不为回文串时,ALICE总是可以选择翻转s),所以ALICE的最优决策为在s变为回文串之前一直翻转s串,这样在s串变为回文串时,BOB就比ALICE多花费了b,最后结合s为回文串时的情况进行特判就行。

当cnt为偶数时,BOB和ALICE的最优决策同上,有所差别的是,ALICE需要在最后一次能使s变为回文串的时候将对应的0变为1使得s成为回文串,这时候问题就变成了BOB先手的0的个数为偶数的回文串游戏,于是容易得出在这个游戏里,BOB始终比ALICE多花费2,又因为在s变为回文串的过程中,ALICE的最多花费为1,所以综上所述:当cnt为偶数并且s初始不是回文串时,始终是ALICE赢,否则BOB赢(即b=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 cnt = 0;
int a = 0,b = 0;
for(int i = 1; i <= n; i++)
{
if(s[i] == '0')cnt++;
}
int l = 0,r = 0;
for(int i = 1; i <= n/2; i++)
if(s[i] != s[n-i+1])
b++;
cnt -= b;
if(cnt % 2)
{
if(b == 1 && cnt == 1)
{
cout<<"DRAW"<<endl;
}
else if(b == 0)
{
if(cnt > 1)
cout<<"ALICE"<<endl;
else
cout<<"BOB"<<endl;
}
else
{
cout<<"ALICE"<<endl;
}
}
else
{
if(b == 0)
{
cout<<"BOB"<<endl;
}
else
{
cout<<"ALICE"<<endl;
}
}
}
}


标签:cnt,Palindrome,string,int,hard,Codeforces,ALICE,BOB,回文
From: https://blog.51cto.com/u_15866543/5829166

相关文章

  • Codeforces Round #713 (Div. 3) F
    F.Education考虑贪心显然我们每次只有这样一种情况就是钱够了就升级然后到一个位置就一直不动了不可能我们先在一个位置钱赚够了再赚几轮再去下一级那么证明我们......
  • 【题解】codeforces 1746B Rebellion
    题意:给定一个只包含0和1的数组a,可以对a进行以下操作:选定两个下标不同的元素ai和aj,将ai加到aj上,再从数组中删除ai。问最少操作多少次,可以让数组a变成单调非下降子序列(即ai......
  • Educational Codeforces Round 113 (Rated for Div. 2) D
    D.InconvenientPairs观察完样例我们发现发现有且仅有一个共同区间的才是一对这样我们直接记录x,y二分出他在哪个区间内check在共同区间的个数即可但是还有另一种解......
  • Codeforces Round #732 (Div. 1) B
    B.AquaMoonandChess简单计数观察样例我们发现如果是00111100这样是11是随便可以放置在任何地方但是要是011100这样的就会有个单独的1出来我们显然可以这样011......
  • Palindrome Partitioning II
    https://leetcode.cn/problems/palindrome-partitioning-ii/dp[i]表示s[0..i]切分为回文子串的最小分割次数dp[i]=min(dp[j]+1),如果s[j+1...i]是回文串,j<......
  • Codeforces Round #832 (Div. 2) D (预处理+二分)
    D.YetAnotherProblem观察题干发现一定要是odd考虑发掘性质发现之后还会将[l,r]长度为奇数的区间全部赋值成这个区间的异或和我们设长度为lenlen-1个偶数个异或为......
  • Codeforces-1753B Factorial Divisibility题解
    Codeforces-1753BFactorialDivisibility参考:https://blog.csdn.net/qq_38236082/article/details/127500190题意:问$a_1!+a_2!+a_3!+...+a_n!$能否被$x!$整除。......
  • CodeForces 1747E List Generation
    CF传送门洛谷传送门考虑将问题抽象成:左上角为\((0,0)\)、右下角为\((n,m)\)的网格图,求所有满足至少有一条只向下或向右走的路径经过点集内所有点的的不同的点集大......
  • Educational Codeforces Round 138 E,F
    E一道比较基础的题。首先就是纵向,走无障碍的格子,无法四联通和横向,走有障碍的格子,八联通是等价的。也就是,如果我们要让其不存在非法路径,那么应该存在一个从第一列出发,一......
  • Educational Codeforces Round 118 D
    D.MEXSequences对于一个数x要是前面出现过0,1,2...x-1我们显然可以将他放在后面要是前面出现过012...x-1x我们也可以将他放在后面但是观察样例还有一种情况......