B - Playing Cards Validation Editorial by HideN2000
正規表現を用いた解法一つ目と二つ目の条件にマッチしているかどうかを判定する際、正規表現を用いると比較的簡潔に行うことができます。
以下はC++とPython3による実装例となります。
[C++]
#include<iostream>
#include<regex>
#include<set>
#include<vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<string> strs(n);
for (int i = 0; i < n; i++){
cin >> strs[i];
}
regex pattern(R"([HDCS][A2-9TJQK])");
if (any_of(strs.begin(), strs.end(), [&](string s) { return !regex_match(s, pattern); } )){
cout << "No" << endl;
}else{
multiset<string> strs_set(strs.begin(), strs.end());
bool duplicated = false;
for (auto & s : strs){
if (strs_set.count(s) > 1){
duplicated = true;
}
}
cout << (!duplicated ? "Yes" : "No") << endl;
}
return 0;
}
[Python3]
import re
pattern = re.compile(r'[HDCS][A2-9TJQK]')
n = int(input())
strs = [input() for i in range(n)]
if any(not pattern.match(s) for s in strs):
print("No")
else:
print("Yes" if len(set(strs)) == n else "No")
posted:
last update: