See this article on my own blog https://dyingdown.github.io/2020/02/14/CodeForeces-1301A-Three-Strings/
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output:standard output
You are given three strings a a a, b b b and c c c of the same length n n n. The strings consist of lowercase English letters only. The i − t h i-th i−th letter of a a a is a i a_i ai, the i − t h i-th i−th letter of b b b is b i b_i bi, the i − t h i-th i−th letter of c c c is c i c_i ci.
For every i i i ( 1 ≤ i ≤ n ) (1≤i≤n) (1≤i≤n) you must swap (i.e. exchange) c i c_i ci with either a i a_i ai or b i b_i bi. So in total you’ll perform exactly n n n swap operations, each of them either c i ↔ a i c_i↔a_i ci↔ai or c i ↔ b i c_i↔b_i ci↔bi ( i i i iterates over all integers between 1 1 1 and n n n, inclusive).
For example, if a a a is “code”, b b b is “true”, and c c c is “help”, you can make c c c equal to “crue” taking the 1 − s t 1-st 1−st and the 4 − t h 4-th 4−th letters from a a a and the others from b b b. In this way a a a becomes “hodp” and b b b becomes “tele”.
Is it possible that after these swaps the string a a a becomes exactly the same as the string b b b?
Input
The input consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 100 ) (1≤t≤100) (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 a a.
The second line of each test case contains a string of lowercase English letters b b b.
The third line of each test case contains a string of lowercase English letters c c 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 100 100.
Output
Print t t t lines with answers for all test cases. For each test case:
If it is possible to make string a a a equal to string b b b print “YES” (without quotes), otherwise print “NO” (without quotes).
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 a a becomes exactly the same as string b b b.
In the second test case, you should swap c i c_i ci with a i a_i ai for all possible i i i. After the swaps a a a becomes “bca”, b b b becomes “bca” and c c c becomes “abc”. Here the strings a a a and b b b are equal.
In the third test case, you should swap c 1 c_1 c1 with a 1 a_1 a1, c 2 c_2 c2 with b 2 b_2 b2, c 3 c_3 c3 with b 3 b_3 b3 and c 4 c_4 c4 with a 4 a_4 a4. Then string a a a becomes “baba”, string b b b becomes “baba” and string c c c becomes “abab”. Here the strings a a a and b b b are equal.
In the fourth test case, it is impossible to do the swaps so that string a a a becomes exactly the same as string b b b.
Analysis
Only two conditions exist that make it impossible:
- For i-th position, all a i a_i ai b i b_i bi and c i c_i ci are not equal to each other.
- a i = b i ≠ c i a_i=b_i \neq c_i ai=bi=ci, so if changed, a i ≠ b i a_i\neq b_i ai=bi
So that are actually one situation: a i ≠ c i a_i \neq c_i ai=ci and b i ≠ c i b_i \neq c_i bi=ci
Code
#include<bits/stdc++.h>
using namespace std;
int arr[2000000];
int main() {
int t;
cin >> t;
while(t --) {
string a, b, c;
cin >> a >> b >> c;
int flag = 1;
for(int i = 0; i < a.length(); i ++) {
if(a[i] != c[i] and b[i] != c[i]) {
flag = 0;
break;
}
}
if(flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}