cf823div2C

题目链接

题目

给你两个字符串\(s_1,s_2\).每次操作可以让\(s_1\)的前k个和\(s_2\)的后k个交换。询问是否可以通过多次上述操作,使得\(s_1=s_2\)。

思路

这种题通过观察,从变中发现不变的部分。可以发现,每次交换。

每个位置\(s1_{i}\)对应位\(s2_{n-i+1}\)是不变的。通过这个性质,我们可以知道对于每个这样的数对,我们必须找到另外一个相同的数对使得他们交叉匹配,才能使\(s1=s2\)有可能成立。对于\(n\)为偶数的情况。每种数对都必须是偶数个才能完全匹配。对于\(n\)为奇数,可以找到一个中间的一个来放奇数的数对,但是这个数对必须是相同字母组成的才可以。

代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#define rep(i,l,n) for(int i=(l);i<=(n);++i)
#define ll long long
#define N 100005
using namespace std;
int t,n;
int a[200][200];
char s1[N],s2[N];
void solve()
{
scanf("%d",&n);
cin>>s1+1>>s2+1;
rep(i,1,n)
{
int j=n-i+1;
int tepa=s1[i],tepb=s2[j];
if(tepa>tepb)swap(tepa,tepb);
a[tepa][tepb]++;
}
int cnt=0;
for(int i='a';i<='z';++i)
{
cnt+=(a[i][i]&1);
for(int j=i+1;j<='z';++j)
{
if(a[i][j]&1)
{
printf("NO\n");
return;
}
}
}
if(((n&1)==0)&&cnt==0)printf("YES\n");
else if((n&1)&&cnt==1)printf("YES\n");
else cout<<"NO\n";
return;
}
int main()
{
cin>>t;
while(t--)
{
memset(a,0,sizeof(a));
solve();
}
system("pause");
return 0;
}

cf823div2C的更多相关文章

随机推荐

  1. zookeeper 选举流程源码解析

    在开始之前,我们先了解一下zookeeper集群角色,zookeeper中存在leader,follower, observer这么几个角色, leader, follower, 就类似与mysql ...

  2. 微信h5支付报错 商家参数格式有误,请联系商家解决

    商家参数格式有误,请联系商家解决 当前调起H5支付的referer为空导致,一般是因为直接访问页面调起H5支付,请按正常流程进行页面跳转后发起支付,或自行抓包确认referer值是否为空 解决:就把 ...

  3. c原因学习---指针作为函数的形参

    指针作为函数的形参, 可以改变实参的值. #include<stdio.h> // 交换两个变量的值 int swap(int x, int y) { int k = y; y = x; ...

  4. phaclon 初学者遇到的问题!

    1,框架安装  需要安装PHALCON扩展. 2,Nginx伪静态 配置 3,app.ini  常量配置等配置 4,主体目录结构 互相调用及 类的注册服务 依赖注入 自动加载项问题. 5,数据库相关操 ...

  5. mysql两表关联

    mysql两表关联 是按照范围关联表 select * from ((select u.id,u.name,u.sex,s.street_name,u.street_code,u.birthday f ...

  6. Screw数据库文档生成神器

    引入依赖 <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-cor ...

  7. JAVA根据时间增加1天

    String time = "2021-12-1"; //指定时间 int day = 30;//指定增加天数 SimpleDateFormat sf = new SimpleDa ...

  8. (Jmeter笔记)jmeter连接数据库(mysql)

    下载mysql连接驱动 地址:https://dev.mysql.com/downloads/connector/j/ ****把mysql连接驱动放在Jmeter/lib目录下**** >&g ...

  9. antDesignVue表格

    <template>       <a-table         :pagination="ipagination"         @change=" ...

  10. leecode编写记录

    记录leecode刷题思路 39.组合总数 39. 组合总和 - 力扣(LeetCode) 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candida ...