Official

B - AtCoder Quiz Editorial by en_translator


The simplest idea is to store four candidates of strings and four flags representing whether they were given in the input or not, and output the one not appeared. In order to compare strings, in C++ for example, one can

  • store them as string type and compare them as strings, or
  • store them in arrays like char[] type and compare them one character by one.

This time, the strings differ only in its second character, and the only possible inputs are ABC , ARC , AGC , or AHC, so one can compare only the second character.

Therefore, this problem can be solved with a if statement.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	string cand[4] = { "ABC", "AGC", "AHC", "ARC" };
	bool used[4] = {};
	string str;
	for (int i = 0; i < 3; i++) {
		cin >> str;
		for (int j = 0; j < 4; j++) {
			if (cand[j] == str)used[j] = true;
		}
	}
	for (int i = 0; i < 4; i++) {
		if (!used[i])cout << cand[i] << endl;
	}
	return 0;
}

Sample code in Python:

cand=["ABC","ARC","AGC","AHC"]
used=[True for i in range(4) ]

for i in range(3):
    str=input()
    for j in range(4):
        if(str==cand[j]):
            used[j]=False

for i in range(4):
    if(used[i]):
        print(cand[i])

By the way, here is a kind of tricky code, which makes use of the fact that the strings differ only in its second character. Note that this may not be applied for some other languages.

Another sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	int k = 0 + 'B' + 'G' + 'H' + 'R';
	string str;
	for (int i = 0; i < 3; i++) {
		cin >> str;
		k -= str[1];
	}
	cout << "A" << (char)k << "C" << endl;
	return 0;
}

posted:
last update: