A15721 | Serval and Inversion Magic
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Serval has a string $s$ that only consists of 0 and 1 of length $n$ . The $i$ -th character of $s$ is denoted as $s_i$ , where $1\leq i\leq n$ .
Serval can perform the following operation called Inversion Magic on the string $s$ :
- Choose an segment $[l, r]$ ( $1\leq l\leq r\leq n$ ). For $l\leq i\leq r$ , change $s_i$ into 1 if $s_i$ is 0, and change $s_i$ into 0 if $s_i$ 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 can perform the following operation called Inversion Magic on the string $s$ :
- Choose an segment $[l, r]$ ( $1\leq l\leq r\leq n$ ). For $l\leq i\leq r$ , change $s_i$ into 1 if $s_i$ is 0, and change $s_i$ into 0 if $s_i$ 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.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1\leq t\leq 10^4$ ). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $2\leq n\leq 10^5$ ) — 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\cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $2\leq n\leq 10^5$ ) — 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\cdot 10^5$ .
输出格式
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).
You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
输入输出样例
输入 #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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted