栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AtCoder Beginner Contest 221 (2021.10.2) B-typo

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AtCoder Beginner Contest 221 (2021.10.2) B-typo

题目大意:

给出两个字符串A,B,问B串是否能通过最多一次操作变成A.
定义一次操作为:交换相邻两个字符。

思路:
首先我们求出有多少位与A串不同,记作cnt。

  1. 如果cnt==0,显然A,B两个串相同,Yes
  2. 如果cnt>2,说明无论怎么操作,A,B两个串都至少会有一位不相同
  3. 如果cnt==2,考虑找到第一位与A串不相同的字符,分别与前后交换,判断是否能变成A即可。(对于一次操作一定是相邻的,那只用判断就可以了,如果不行,显然两个不同字符的位置不相邻,无法操作)。
#include 
#include 
#include 

using namespace std;

int len, cnt;
string a, b, d;
char c;

int main()
{
	cin >> a >> b;
	len = a.length();
    for(int i = 0; i < len; i++) if(a[i] != b[i]) cnt++;
    if(cnt == 0) {printf("Yesn"); return 0;}
    if(cnt > 2 || cnt == 1) printf("Non");
    else
    {
        for(int i = 0; i < len; i++)
        {
            if(b[i] != a[i])
            {
                d = b;
                c = d[i];
                d[i] = d[i - 1];
                d[i - 1] = c;
                if(d == a) {printf("Yesn"); return 0;}
                d = b;
                c = d[i];
                d[i] = d[i + 1];
                d[i + 1] = c;
                if(d == a) {printf("Yesn"); return 0;}
                printf("Non"); 
                return 0;
            }
        }
    }
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/289466.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号