CF1789B.Serval and Inversion Magic

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Serval has a string ss that only consists of 0 and 1 of length nn. The ii-th character of ss is denoted as sis_i, where 1≤i≤n1\leq i\leq n.

Serval can perform the following operation called Inversion Magic on the string ss:

  • Choose an segment [l,r][l, r] (1≤l≤r≤n1\leq l\leq r\leq n). For l≤i≤rl\leq i\leq r, change sis_i into 1 if sis_i is 0, and change sis_i into 0 if sis_i is 1.

For example, let ss be 010100 and the segment [2,5][2,5] is chosen. The string ss will be 001010 after performing the Inversion Magic.

Serval wants to make ss a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible.

A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not.

Serval 有一个长度为 nn 的字符串 ss,该字符串仅由字符 0 和 1 组成。字符串 ss 的第 ii 个字符记作 sis_i,其中 1≤i≤n1\leq i\leq n。

Serval 可以对字符串 ss 执行如下操作,称为“翻转魔法”(Inversion Magic):

  • 选择一个区间 [l,r][l, r](满足 1≤l≤r≤n1\leq l\leq r\leq n)。对每个满足 l≤i≤rl\leq i\leq r 的下标 ii,将 sis_i 替换为:若 sis_i 是 0 则变为 1,若 sis_i 是 1 则变为 0。

例如,设 s=010100s = \texttt{010100},并选择区间 [2,5][2,5],则执行翻转魔法后,字符串 ss 将变为 001010\texttt{001010}。

Serval 希望恰好执行一次翻转魔法操作,使得 ss 变为回文串。请你帮他判断这是否可行。

一个字符串是回文串,当且仅当它正读与反读完全相同。例如,010010\texttt{010010} 是回文串,而 10111\texttt{10111} 不是。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041\leq t\leq 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤1052\leq n\leq 10^5) — the length of string ss.

The second line of each test case contains a binary string ss of length nn. Only characters 0 and 1 can appear in ss.

It's guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041\leq t\leq 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052\leq n\leq 10^5)—— 字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。字符串 ss 中仅可出现字符 0 和 1。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, print Yes if ss can be a palindrome after performing Inversion Magic exactly once, and print No if not.

You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

对于每个测试用例,如果字符串 ss 在恰好执行一次“反转魔法”(Inversion Magic)后可以成为回文串,则输出 Yes;否则输出 No。

Yes 和 No 的大小写不限(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    3
    4
    1001
    5
    10010
    7
    0111011

    输出#1

    Yes
    Yes
    No

说明/提示

In the first test case, Serval can perform Inversion Magic on the segment [1,4][1,4]. The string ss will be 0110 after the magic.

In the second test case, Serval can perform Inversion Magic on the segment [1,3][1,3]. The string ss will be 01110 after the magic.

In the third test case, Serval can't make ss a palindrome by performing Inversion Magic exactly once.

在第一个测试用例中,Serval 可以对区间 [1,4][1,4] 执行“反转魔法”。执行魔法后,字符串 ss 将变为 0110。

在第二个测试用例中,Serval 可以对区间 [1,3][1,3] 执行“反转魔法”。执行魔法后,字符串 ss 将变为 01110。

在第三个测试用例中,Serval 无法通过恰好执行一次“反转魔法”使 ss 成为回文串。

输入解题思路,AI测评打分。不知道怎么写?

首页