codeforce 721 div2 B2. Palindrome Game (hard version)

B2. Palindrome Game (hard version)

题意

给定一个字符串(只包含0和1),ALICE和BOB轮流操作,当字符串全为1时,游戏结束

  • 操作1: 将其中的一个0换成1,代价为1
  • 操作2:反转字符串,前提是当前字符串不是一个回文串,并且上一个人的操作不是反转字符串
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B2 {

    private int n;
    private String s;

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        B2 b2 = new B2();
        // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for(int i = 0; i < t; i++) {
            b2.read();
            b2.solve();
        }
    }

    // 3
    // 110
    // ALICE

    // 2
    // 00
    // BOB

    // 4
    // 1010
    // ALICE
    private void solve() {
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            if(s.charAt(i) == '0') {
                cnt++;
            }
        }
        if(check()) {
            // 如果是回文串
            if((cnt & 1) == 1 && cnt > 1) System.out.println("ALICE");
            else System.out.println("BOB");
        } else {
            // 如果不是回文串
            // 如果0的个数为2个,并且n为奇数,中间的字符为0,导致了反转一次可以变成回文串
            // 10110
            // 10011
            // 这样的话ALICE和BOB各pay 1
            if(cnt == 2 && s.charAt(s.length() / 2) == '0' && s.length() % 2 == 1) {
                System.out.println("DRAW");
            } else {
                // 如果可以将字符串变成回文串
                // cnt:偶数 ALICE 选择反转,让BOB执行这一步
                // cnt:奇数 ALICE 执行操作一,类似于之前一开始就是回文串且cnt为奇数的情况相同
                System.out.println("ALICE");
            }
        }
    }

    private Boolean check() {
        for(int i = 0; i < n / 2; i++) {
            if(s.charAt(i) != s.charAt(n - i - 1)) {
                return false;
            }
        }
        return true;
    }

    private void read() throws IOException {
        n = Integer.parseInt(br.readLine());
        s = br.readLine();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值