B. Serval and Inversion Magic

Problem - B - Codeforces

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≤n1≤�≤�.

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

  • Choose an segment [l,r][�,�] (1≤l≤r≤n1≤�≤�≤�). 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][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.

Input

Each test contains multiple test cases. The first line contains the number of test cases t� (1≤t≤1041≤�≤104). The description of the test cases follows.

The first line of each test case contains a single integer n� (2≤n≤1052≤�≤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⋅1052⋅105.

Output

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).

Example

input

Copy

 

3

4

1001

5

10010

7

0111011

output

Copy

Yes
Yes
No

Note

In the first test case, Serval can perform Inversion Magic on the segment [1,4][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][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.

题意:

反转一段只含01的字符串,若反转后为回文则输出yes,否则no。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//int k = min_element(a + 1, a + 1 + n) - a
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int a[N],bb[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<int,char>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
//struct Range
//{
//    int l,r;
//    bool operator< (const Range &w)const
//    {
//        return l<w.l;
//    }
//}range[N];
int n;
string s;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
void solve()
{
    cin>>n;
    cin>>s;
    int f=0;
    int i,j;
    for( i=0,j=n-1;i<j;)
    {
        if(s[i]==s[j]&&!f)
        {
            i++;
            j--;
        }
        if(s[i]!=s[j])
        {
            f=1;
            i++;
            j--;
        }
        if(s[i]==s[j]&&f)
        {
            break;
        }
        
    }
   string ss=s.substr(i,j-i+1);
    string sss=ss;
   reverse(ss.begin(),ss.end());
  
   if(ss==sss)
   {
       YES
   }
   else NO
}
signed main()
{
   int t=1;
   cin>>t;
   while(t--)
   {
       solve();
   }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值