CF1789B.Serval and Inversion Magic
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Serval has a string s that only consists of 0 and 1 of length n. The i-th character of s is denoted as si, where 1≤i≤n.
Serval can perform the following operation called Inversion Magic on the string s:
- Choose an segment [l,r] (1≤l≤r≤n). For l≤i≤r, change si into 1 if si is 0, and change si into 0 if si is 1.
For example, let s be 010100 and the segment [2,5] is chosen. The string s will be 001010 after performing the Inversion Magic.
Serval wants to make s 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 有一个长度为 n 的字符串 s,该字符串仅由字符 0 和 1 组成。字符串 s 的第 i 个字符记作 si,其中 1≤i≤n。
Serval 可以对字符串 s 执行如下操作,称为“翻转魔法”(Inversion Magic):
- 选择一个区间 [l,r](满足 1≤l≤r≤n)。对每个满足 l≤i≤r 的下标 i,将 si 替换为:若 si 是
0则变为1,若 si 是1则变为0。
例如,设 s=010100,并选择区间 [2,5],则执行翻转魔法后,字符串 s 将变为 001010。
Serval 希望恰好执行一次翻转魔法操作,使得 s 变为回文串。请你帮他判断这是否可行。
一个字符串是回文串,当且仅当它正读与反读完全相同。例如,010010 是回文串,而 10111 不是。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤105) — the length of string s.
The second line of each test case contains a binary string s of length n. Only characters 0 and 1 can appear in s.
It's guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤105)—— 字符串 s 的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s。字符串 s 中仅可出现字符 0 和 1。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print Yes if s 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).
对于每个测试用例,如果字符串 s 在恰好执行一次“反转魔法”(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]. The string s will be 0110 after the magic.
In the second test case, Serval can perform Inversion Magic on the segment [1,3]. The string s will be 01110 after the magic.
In the third test case, Serval can't make s a palindrome by performing Inversion Magic exactly once.
在第一个测试用例中,Serval 可以对区间 [1,4] 执行“反转魔法”。执行魔法后,字符串 s 将变为 0110。
在第二个测试用例中,Serval 可以对区间 [1,3] 执行“反转魔法”。执行魔法后,字符串 s 将变为 01110。
在第三个测试用例中,Serval 无法通过恰好执行一次“反转魔法”使 s 成为回文串。
输入解题思路,AI测评打分。不知道怎么写?