Codeforces Round #619 (Div. 2):A. Three Strings

Discription
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is ai, the i-th letter of b is bi, the i-th letter of c is ci.

For every i (1≤i≤n) you must swap (i.e. exchange) ci with either ai or bi. So in total you’ll perform exactly n swap operations, each of them either ci↔ai or ci↔bi (i iterates over all integers between 1 and n, inclusive).

For example, if a is “code”, b is “true”, and c is “help”, you can make c equal to “crue” taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes “hodp” and b becomes “tele”.

Is it possible that after these swaps the string a becomes exactly the same as the string b?

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a string of lowercase English letters a.

The second line of each test case contains a string of lowercase English letters b.

The third line of each test case contains a string of lowercase English letters c.

It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.

Output
Print t lines with answers for all test cases. For each test case:

If it is possible to make string a equal to string b print “YES” (without quotes), otherwise print “NO” (without quotes).

扫描二维码关注公众号,回复: 9203317 查看本文章

You can print either lowercase or uppercase letters in the answers.

Example
input

4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim

output

NO
YES
YES
NO

Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.

In the second test case, you should swap ci with ai for all possible i. After the swaps a becomes “bca”, b becomes “bca” and c becomes “abc”. Here the strings a and b are equal.

In the third test case, you should swap c1 with a1, c2 with b2, c3 with b3 and c4 with a4. Then string a becomes “baba”, string b becomes “baba” and string c becomes “abab”. Here the strings a and b are equal.

In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.

题意
给定三个长度相等的字符串a,b,c,要求执行n次某操作:将ci与ai或者bi进行交换,必须执行。
问n次操作后能否得到a串等于b串。

思路
直接分析若在第i位上,ai,bi,ci都不行等则一定不成立,若,ai,bi相同,但是ci与ai和bi都不相同,则也不成立。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define rep(i, a, n) for(int i=a; i<=n; i++)
#define per(i, n, a) for(int i=n; i>=a; i--)
 
int T;
string a,b,c;
int f;
 
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        cin>>a>>b>>c;
        f=0;
        for(int i=0;i<a.size();i++)
        {
            if(a[i]!=b[i]&&a[i]!=c[i]&&b[i]!=c[i])
            {
                f=1;
                break;
            }
            else if(a[i]==b[i]&&c[i]!=a[i]&&c[i]!=b[i])
            {
                f=1;
                break;
            }
        }
        if(f==1)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
    return 0;
}
发布了313 篇原创文章 · 获赞 105 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43460224/article/details/104311886