๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/49993
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
ํ์ด
1๏ธโฃ queue
1. ์ํ๋ฒณ ๊ฐ์(26) ํฌ๊ธฐ์ ๋ฐฐ์ด๋ก ์์์ ์๊ด์๋ ์คํฌ(true)๊ณผ ์ ํ ์คํฌ์ด ํ์ํ ์คํฌ(false)์ ํ์ํ๋ค.
2. ์คํฌ์ ์์ ๋น๊ต๋ฅผ ์ํด ํ๋ฅผ ์์ฑํ์ฌ skill ๊ฐ์ ํ ๋นํ๋ค.
3. skill_trees ๋ด์ ๋ฌธ์์ด ํ์
(1) ์ ํ ์คํฌ์ด ํ์์๋ ์คํฌ์ ๋ฌด์ํ๋ค.
(2) ์ ํ ์คํฌ์ด ํ์ํ ์คํฌ์ผ ๊ฒฝ์ฐ, ํ๋ฅผ ์ด์ฉํด ์์๊ฐ ๊ฐ์์ง ํ์ธํ๋ค. (queue.front()์ ๋น๊ต)
- ์์๊ฐ ๊ฐ์ผ๋ฉด ํ์์ ํด๋น ๋ฌธ์๋ฅผ ์ ๊ฑฐํ๋ค.
- ์์๊ฐ ๋ค๋ฅด๋ฉด answer - 1 ํ ํ, ๋ค์ ๋ฌธ์์ด์ ํ์ํ๋ค.
๐จ skill_trees์ ๋ฌธ์์ด์ ํ์ํ ๋๋ง๋ค ํ๋ฅผ ์ด๊ธฐํํด์ผ ํ๋ค.
2๏ธโฃ string::find()
1. skill_trees ๋ด์ ๋ฌธ์์ด์ ํ์ํ๋ฉด์ skill๊ณผ ๊ฐ์ ๋ฌธ์๊ฐ ์๋์ง string์ find ํจ์๋ฅผ ์ด์ฉํด ์ฐพ๋๋ค.
๊ฐ์ ๋ฌธ์๊ฐ ์์ผ๋ฉด ์ด๋ฅผ string s์ ์ ์ฅํ๋ค.
2. s์ skill์ ์์๋ฅผ ๋น๊ตํ๋ค.
์์๊ฐ ๋ค๋ฅด๋ฉด answer - 1 ํ ํ, ๋ค์ ๋ฌธ์์ด์ ํ์ํ๋ค.
๐จ skill_trees์ ๋ฌธ์์ด์ ํ์ํ ๋๋ง๋ค ๊ฐ์ ๊ฐ์ ์ ์ฅํ๋ string s๋ฅผ ์ด๊ธฐํํด์ผ ํ๋ค.
๐จ ์์๋ฅผ ๋น๊ตํ ๋ skill์ด ์๋ s์ ๊ธธ์ด๋งํผ ๋น๊ตํด์ผ ํ๋ค. ์ ํ ์คํฌ ๋ด์ ๋ชจ๋ ์คํฌ์ ๋ฐฐ์ฐ์ง ์์์ด๋, ์์๋ฅผ ๋ฐ๋ฅด๋ฉด ์กฐ๊ฑด์ ๋ง์กฑํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ฝ๋
1๏ธโฃ queue
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int solution(string skill, vector<string> skill_trees) {
int answer = skill_trees.size();
vector<bool> alphabet(26, true); // ์์์ ์๊ด์๋ ์คํฌ ์ ์ฅ
for (char c: skill) {
alphabet[c - 'A'] = false; // ์ ํ ์คํฌ ํ์ํ ์คํฌ: false
}
for (string skill_tree: skill_trees) {
queue<char> skill_queue; // ์ ํ ์คํฌ
for (char c: skill) { // ์ ํ ์คํฌ ์ฝ์
skill_queue.push(c);
}
for (char c: skill_tree) {
if (alphabet[c - 'A']) { // ์ ํ ์คํฌ ํ์ ์์
continue;
} else if (c == skill_queue.front()) { // ์ ํ ์คํฌ ์กฐ๊ฑด ๋ง์กฑ
skill_queue.pop();
} else { // ์ ํ ์คํฌ ์กฐ๊ฑด ๋ถ๋ง์กฑ
answer--;
break;
}
}
}
return answer;
}
2๏ธโฃ string::find()
#include <iostream>
#include <vector>
using namespace std;
int solution(string skill, vector<string> skill_trees) {
int answer = skill_trees.size();
string s = "";
for(string skill_tree: skill_trees){
for(char c: skill_tree){
// skill์ skill_tree์ ๊ฐ์ ๊ฐ์ด ์์ผ๋ฉด ์ ์ฅ
if(skill.find(c) != string::npos){
s.push_back(c);
}
}
for(int i = 0; i < s.length(); i++){
if(skill[i] != s[i]){ // ์์๊ฐ ๋ค๋ฅด๋ฉด ๋ถ๊ฐ๋ฅํ ์คํฌํธ๋ฆฌ
answer--;
break;
}
}
s = ""; // ์ด๊ธฐํ
}
return answer;
}
'๐ญ Problem Solving > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค][C++] ํฐ ์ ๋ง๋ค๊ธฐ (0) | 2024.06.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค][C++] ํ๊ฒ ๋๋ฒ (0) | 2024.05.31 |
[ํ๋ก๊ทธ๋๋จธ์ค][C++] ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2024.05.27 |
[BOJ][C++] ๋ฐฑ์ค 20546๋ฒ: ๐ ๊ธฐ์ ์ ๋งค๋งค๋ฒ ๐ (0) | 2024.05.26 |
[ํ๋ก๊ทธ๋๋จธ์ค][C++] ์ฒด์ก๋ณต (0) | 2024.05.24 |