Official

B - Playing Cards Validation Editorial by en_translator


This problem can be solved by carefully expressing the given conditions with for statements and if statements. There are some tips in the implementation.

Processing “all conditions are satisfied”

Here are some examples of the approach. (This is merely an example. Any approach that works properly is valid.)

  • Store the variable corresponding to the answer, initialized with True, then check each of the conditions. If one of them is found violated, set the variable to False.
  • Check each of the conditions. If one of them is found violated, print No and terminate the program. If the execution reached the end, print Yes.

Regarding the second condition

Naive interpretation may be like:

for (int i = 0; i < n; i++) {
	if (s[i][1] == 'A' or s[i][1] == '2' or s[i][1] == '3' or s[i][1] == '4' or s[i][1] == '5' or s[i][1] == '6' or s[i][1] == '7' or s[i][1] == '8' or s[i][1] == '9' or s[i][1] == 'T' or s[i][1] == 'J' or s[i][1] == 'Q' or S[i][1] == 'K') {
		// Do the process
	}
}

or

for (int i = 0; i < n; i++) {
	if (s[i][1] != 'A' and s[i][1] != '2' and s[i][1] != '3' and s[i][1] != '4' and s[i][1] != '5' and s[i][1] != '6' and s[i][1] != '7' and s[i][1] != '8' and s[i][1] != '9' and s[i][1] != 'T' and s[i][1] != 'J' and s[i][1] != 'Q' and S[i][1] != 'K') {
		// Do the process
	}
}

but enumerating many conditions like this requires many typing and may lead to mistakes, so we recommend using alternative way.
For example, here is a concise sample in C++:

string s2 = "A23456789TJQK";
for (int i = 0; i < n; i++) {
	if (!count(s2.begin(), s2.end(), s[i][1])) // Do the process
}

and one in Python:

s2 = "A23456789TJQK"
for i in range(n):
	if !s2.count(s[i][1]):
		# Do the process

(again, there are many other concise approaches). Of course, we can do the same way for the first condition, too.

Regarding the \(3\)-rd condition

Of course we can process fast using a set, but note that we can just use double nested loop due to the sufficiently small Constraints. Although it does not really matter in this problem, it is a good strategy in competitive programming to escape to a simple implementation, sacrificing the complexity for some extent. Also, we may fix the order of \(i\) and \(j\) due to the symmetry of these two variables.

Sample code (C++)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<string> s(n);
	for (int i = 0; i < n; i++) cin >> s[i];
	bool ans = true;
	for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) if (s[i] == s[j]) ans = false;
	string s1 = "HDCS";
	string s2 = "A23456789TJQK";
	for (int i = 0; i < n; i++) {
		if (!count(s1.begin(), s1.end(), s[i][0]) || !count(s2.begin(), s2.end(), s[i][1])) ans = false;
	}
	cout << (ans ? "Yes" : "No") << '\n';
}

Sample code (Python)

n = int(input())
s = [input() for _ in range(n)]
s1 = "HDCS"
s2 = "A23456789TJQK"
ans = True
for i in range(n):
	for j in range(i):
		if s[i] == s[j]:
			ans = False
for i in range(n):
	if not s1.count(s[i][0]) or not s2.count(s[i][1]):
		ans = False
print("Yes" if ans else "No")

posted:
last update: