- toc {:toc}
ํ์ด
๋ฐฑํธ๋ํน ๋ฌธ์ ๋ฅผ ๋ง์ด ์ ํ์ด๋ด์ ๊ตฌ์กฐ๊ฐ ์ต์ํ์ง ์์๋ค. ์ถํ์ ๋ค์ ํ์ด๋ด์ผ๊ฒ ๋ค.
- ์ ์ฒด์ ์ธ ๊ตฌ์กฐ๋ ์๋ ๋ฌธ์ฅ๊ณผ ๋ฆฌํธ ๋ฌธ์ฅ์ ํ๋ํ๋ ์ ๋ถ ์ฐ๊ฒฐ์์ผ๋ณด๋ ๋ฐฉ์์ด๋ค.
- ์ฐ๊ฒฐ์ํค๋ ๊ณผ์ ์์ ์ฌ๋ฐ๋ฅธ ์ฐ๊ฒฐ์ด ์๋๋ผ๋ฉด ๋๋์์ค๋ ๋ฐฑํธ๋ํน ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
- ์ฌ๋ฐ๋ฅธ ์ฐ๊ฒฐ์ด ์๋ ๊ฒฝ์ฐ๋ ๋ฆฌํธ ์ฐ๊ฒฐ์ ํ ์ํ๋ฒณ์ด ๋ค์ ๋ํ๋ฌ์ ๋ ์ฐ๊ฒฐ๋ ๋ฆฌํธ ํํ๊ณผ ์ํ๋ฒณ์ด ๊ฐ๋ฆฌํค๋ ๋ฆฌํธ ํํ์ด ๋์ผํ์ง ์ฌ๋ถ์ด๋ค. ex)aa โ @@ ์ธ ๊ฒฝ์ฐ ์ฌ๋ฐ๋ฅธ ํํ, aa โ @! ์ธ ๊ฒฝ์ฐ ์ฌ๋ฐ๋ฅด์ง ์์ ์ฐ๊ฒฐ
- o_idx์ l_idx๊ฐ ๋์์ ๋์ ๋๋ฌํ๋ค๋ฉด ๋ชจ๋ ์ฐ๊ฒฐ๋ ์ํ์ด๋ฏ๋ก true ๋ฐํ
- ๋ ์ค ํ๋๋ง ๋์ ๋๋ฌํ๋ฉด ์ฐ๊ฒฐ์ด ์ฌ๋ฐ๋ฅด์ง ์์ ์ํ์ด๋ฏ๋ก false ๋ฐํ
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int k;
string original, leet_word;
string leet_mapping[30];
bool isPossible(int o_idx, int l_idx){
if(o_idx == original.length() && l_idx == leet_word.length()){
return true;
}
if(o_idx == original.length() || l_idx == leet_word.length()){
return false;
}
int alpha_idx = original[o_idx]-'a';
if(leet_mapping[alpha_idx] == ""){ // ๋ฆฌํธ ํํ์ด ์ฐ๊ฒฐ๋์ง ์์ ๊ฒฝ์ฐ
for(int i = 1; i <= k && l_idx + i <= leet_word.length(); i++){
leet_mapping[alpha_idx] = leet_word.substr(l_idx, i);
if(isPossible(o_idx+1, l_idx+i)){
return true;
}
}
leet_mapping[alpha_idx] = ""; // ๋๋์ ์ค๋ ๊ณผ์ ์์ ์ด๊ธฐํ
}
else{ // ๋ฆฌํธ ํํ์ด ์ฐ๊ฒฐ๋์ด ์๋ ๊ฒฝ์ฐ
string mapped_leet = leet_mapping[alpha_idx];
if(mapped_leet == leet_word.substr(l_idx, mapped_leet.length())){
if(isPossible(o_idx+1, l_idx+mapped_leet.length())){
return true;
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T;
cin >> T;
while(T--){
fill(leet_mapping, leet_mapping+30, "");
cin >> k >> original >> leet_word;
bool res = isPossible(0, 0);
if(res) cout << 1 << endl;
else cout << 0 << endl;
}
return 0;
}