Official
B - Similar String Editorial
by
B - Similar String Editorial
by
nok0
AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
この問題では、まず二つの文字が似た文字かの判定を行うことが必要です。これは、 if 文を用いて実装できます。
次に、文字列に含まれる各文字について似た文字かを比較する必要があります。これは、for 文を用いて実装できます。
具体的な実装方法は実装例をご確認ください。
実装例(C++):
#include <bits/stdc++.h>
using namespace std;
bool sim(char c, char d) {
return c == d or (c == '0' and d == 'o') or (c == 'o' and d == '0') or (c == 'l' and d == '1') or (c == '1' and d == 'l');
}
int main() {
int n;
string s, t;
cin >> n >> s >> t;
bool string_sim = 1;
for(int i = 0; i < n; i++) string_sim &= sim(s[i], t[i]);
cout << (string_sim ? "Yes\n" : "No\n");
}
実装例(Python):
def is_sim_char(c, d):
return (
c == d
or (c == "0" and d == "o")
or (c == "o" and d == "0")
or (c == "l" and d == "1")
or (c == "1" and d == "l")
)
def is_sim_string(s, t):
for i in range(len(s)):
if not is_sim_char(s[i], t[i]):
return False
return True
n = int(input())
s = input()
t = input()
print("Yes" if is_sim_string(s, t) else "No")
posted:
last update: