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();}
}

B. Serval and Inversion Magic相关推荐

  1. Python 的 Magic Methods 指南(转)

    介绍 本指南是数月博客的总结.主题是魔术方法. 什么是魔术方法呢?它们是面向对象Python语言中的一切.它们是你可以自定义并添加"魔法"到类中的特殊方法.它们被双下划线环绕(比如 ...

  2. (转)python类:magic魔术方法

    原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...

  3. Python 魔法方法(pythonzho 的 Magic Methods 指南)

    介绍 本指南是数月博客的总结.主题是魔术方法. 英文原文:A Guide to Python's Magic Methods 什么是魔术方法呢?它们是面向对象Python语言中的一切.它们是你可以自定 ...

  4. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  5. UVA 11990 ``Dynamic'' Inversion 动态逆序对

    ``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...

  6. 我花了三个小时写了一道题的六千字题解....(POJ 2888 Magic Bracelet)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 前置知识:小学生都能看懂的群论从入门到升天教程 <群论全家桶> 一道简单的题目 Probl ...

  7. 安装报错_RG Magic Bullet安装报错修复方法

    RG Magic Bullet安装报错修复方法 安装和谐版红巨星插件发现有很大的概率报错.这几天重装电脑被这个插件搞得头大.网上查了一些排除错误的方法,基本错误可以靠删除一些重复的文件夹和老版本来修复 ...

  8. magic系统将来能升鸿蒙,荣耀30和V30将首批搭载Magic UI 4.0 后续可升级鸿蒙系统

    腾讯科技讯 9月10日下午15点30分,华为消费者业务软件部总裁王成录在华为开发者大会上发表了题为<连接无限可能-全场景终端软件发布>的主题演讲,EMUI 11和Magic UI 4.0同 ...

  9. 全球首个Magic Leap One体验:吓到你不敢进房间

    来源:智东西 概要:业内备受关注的AR技术公司Magic Leap,在获得19亿美元融资历经七年之后,终于放出其第一款头盔产品Magic Leap One,很快在科技圈.VR圈引起刷屏式关注. 昨夜, ...

最新文章

  1. 利用Use Case为系统行为建模(1)
  2. BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
  3. 自学python需要多长时间-Python学习步骤如何安排?多长时间可以学会精通呢?
  4. python人脸识别、人脸关键点检测、性别检测
  5. 2016rMBP登录后自动关机、帐户数据丢失
  6. Sentinel集群流控
  7. CentOS6.0 yum php mcrypt 扩展安装问题
  8. java 线程池的理解_JAVA线程池原理的理解
  9. 膨胀卷积的缺点_膨胀卷积与IDCNN
  10. 笨方法学python第四版当当_“笨办法”学Python(第3版)
  11. Linux系统中提示/usr/bin/ld: cannot find -lxxx错误的通用解决方法
  12. [译] 使用 iPhone X 与 Maya 实现快速面部捕捉
  13. 备案 前置或专项审批的内容
  14. excel粘贴为图片不完整_excel转PDF不完整?办公大神的压箱绝技来了!
  15. 安装svn 汉化包 也不能设置中文
  16. 数据库常见面试题(附答案)
  17. 想知道北京的公交线路图吗?用python给你画出来
  18. postgresql 高可用 repmgr 的使用之四 1 Primary + 1 Standby 的 switchover
  19. 第4章 系统“后悔药”--vmware的快照功能
  20. 尚硅谷01 数据结构与算法_数据结构与算法介绍+稀疏数组

热门文章

  1. python怎么输入小数啊_如何在python中打小数点
  2. Halcon中$的使用
  3. 从 0 到 1 的 VR 界面设计之路
  4. BGP在数据中心的应用6——BGP在服务器上的应用
  5. python用函数绘制椭圆_详解opencv中画圆circle函数和椭圆ellipse函数
  6. Web界面设计(Designing Web Interfaces中文版) (美)斯科特 pdf扫描版​
  7. ❤️小程序入门基础(二)❤️(个人学习笔记)
  8. 【第3版emWin教程】第51章 emWin6.x的Window窗口控件
  9. Unity3d 移除某个GameObject的所有子物体
  10. UG12.0安装完成后Application failed to start because it could not find or load the QT platform的解决办法