• Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)


    This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.

    After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.

    Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions ii and jj (1i,jn1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj. Can he succeed?

    Note that he has to perform this operation exactly once. He has to perform this operation.

    Input

    The first line contains a single integer kk (1k101≤k≤10), the number of test cases.

    For each of the test cases, the first line contains a single integer nn (2n1042≤n≤104), the length of the strings ss and tt.

    Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

    Output

    For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise.

    You can print each letter in any case (upper or lower).

    Example
    input
    Copy
    4
    5
    souse
    houhe
    3
    cat
    dog
    2
    aa
    az
    3
    abc
    bca
    
    output
    Copy
    Yes
    No
    No
    No
    
    Note

    In the first test case, Ujan can swap characters s1s1 and t4t4, obtaining the word "house".

    In the second test case, it is not possible to make the strings equal using exactly one swap of sisi and tjtj.

    #include<bis/stdc++.h>
    using namespace std;
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            int n;
            scanf("%d",&n);
            string s,t;
            cin>>s;
            cin>>t;
            int c1=-1,c2=-1;
            int flag = 0;
            int sum=0;
            for(int i = 0; i < n; i++) {
                if(s[i] != t[i]) {
                    sum++;
                    if(sum == 1) {  //记录不同的位置
                        c1 = i;
                    } else if(sum == 2) {
                        c2 = i;
                    } else {
                        flag = 1;//两对以上,直接结束
                        break;
                    }
                }
            }
            if(flag == 1) {
                printf("No
    ");
                continue;
            }
            if(s[c1] == s[c2]&&t[c1] == t[c2]) {
                printf("Yes
    ");
            } else {//字母不同
                printf("No
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Java工程代码开发规范总结
    MySQL5.7 JSON字段性能测试
    Forest v1.5.12 发布,声明式 HTTP 框架,已超过 1.6k star
    HTTP基础系列之:一文搞懂URL
    近1000 star,Forest 1.5.0 正式版发布
    我的开源项目在五个月内超过了 600 star
    我的开源经历:为了方便处理三方 HTTP 接口而写的 Java 框架
    【约瑟夫环】C语言数组法+java循环链表法
    UNICODE编码 特殊符号
    swift3.0 保存图片到本地,申请权限
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829330.html
Copyright © 2020-2023  润新知