โ 10815 ์ซ์ ์นด๋ โ
//10815
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N,M,card,judge;
vector<int> cards;
vector<int> judges;
map<int,bool> Map;
cin >> N;
while(N--){
cin >> card;
Map.insert({card, true});
}
cin >> M;
while(M--){
cin >> judge;
if(Map[judge]){
cout << "1 ";
}
else{
cout << "0 ";
}
}
return 0;
}
๐ฝ Map์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ c++์์ ๋ง๋ค ๋ vector์ ๋น์ทํ๊ฒ map<int, bool> Map์ด๋ ๊ฒ key๊ฐ์ intํ, value๊ฐ์ boolํ์ธ, ์ด๋ฆ์ Map์ธ ๋งต ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๋ค. / Map์ ์๋ฃ๋ฅผ ๋ฃ์ ๋๋ Map.insert({key, value}) ์ด๋ ๊ฒ ๋ฃ์ ์ ์๋ค.
๐ฝ ์กด์ฌํ์ง ์๋ key๋ฅผ map์ ์ ๊ทผํ ๋ ํด๋น key์ value๋ก false๊ฐ ์๋ ํ ๋น๋๋ค. Map์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๊ฐ ์ฒ์ ๋ง๋ค์ด์ง ๋ default๋ก false ํ ๋น์ด ์๋๋ค.
โ 14425 ๋ฌธ์์ด ์งํฉ โ
//14425
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N,M,ans=0;
string str_a, str_b;
set<string> Set;
cin >> N >> M;
while(N--){
cin >> str_a;
Set.insert(str_a);
}
while(M--){
cin >> str_b;
if(Set.find(str_b) != Set.end()){
ans+=1;
}
}
cout << ans;
return 0;
}
๐ฝ set๋ผ๋ ์งํฉ ์๋ฃ๊ตฌ์กฐ๋ #include <set>๋ก ๊ตฌํ ๊ฐ๋ฅ. set<String> Set์ผ๋ก ์งํฉ ์์ ๋ค์ด๊ฐ ๋ฐ์ดํฐ ํ์ ์ <>์์ ๋ฏธ๋ฆฌ ๋ฃ์ ์ ์๋ค.
๐ฝ insert()๋ก ์์๋ฅผ ๋ฃ๊ณ , ์ํ๋ ์์๊ฐ set์ ์๋ ์ง ์ฐพ์ผ๋ ค๋ฉด find() ํจ์ ํ์ฉ. find() ํจ์ ๊ฒฐ๊ณผ ์กด์ฌํ์ง ์๋ ์์๋ผ๋ฉด Set_์ด๋ฆ.end() ๋ฐํ.
โ 7785 ํ์ฌ์ ์๋ ์ฌ๋ โ
//7785
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string worker, status;
map<string, string, greater<string>> workers;
cin >> n;
while(n--){
cin >> worker >> status;
if(status == "enter"){
workers.insert({worker, status});
}
else{ // status == "leave"
workers.erase(worker);
}
}
for(const auto&worker : workers){
cout << worker.first << '\n';
}
return 0;
}
๐ฝ c++์ map์ ๊ธฐ๋ณธ์ ์ผ๋ก key ์ค๋ฆ์ฐจ์ ํ์ฑ์ด๋ค. ๋ฐ๋ผ์, key ๋ด๋ฆผ์ฐจ์์ผ๋ก ํ์ฑ๋๊ฒ map ๋ง๋ค๋ ค๋ฉด map<>์ ์ธ๋ฒ์งธ ์ธ์์ greater<string> ์ฝ์ .
๐ฝ status ๊ฐ์ด "enter"๋ผ๋ฉด insert(), "leave"๋ผ๋ฉด erase(worker)๋ก key๊ฐ์ ์ฌ์ฉํด map์ <key, value> ์์ ์ง์ด๋ค.
๐ฝ map์ for๋ฌธ์ผ๋ก ๋๋ฆฌ๋ ๋ฐฉ๋ฒ ์ค ํ๋๋ const auto& ์ฐธ์กฐ ๋ณ์๋ฅผ ํ์ฉํด ์ฃผ์๊ฐ์ผ๋ก map์ <key,value>๊ฐ์ ๊ฐ๋ฅดํค๋ฉฐ iteration ์งํํ ์ ์๋ค. ์ด ๋์ iteration ์งํ ๋ฐ๋ณต ๋ณ์์ first๋ key๊ฐ, second๋ value๊ฐ์ด๋ค.
โ 11478 ์๋ก ๋ค๋ฅธ ๋ถ๋ถ ๋ฌธ์์ด์ ๊ฐ์ โ
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string S;
set<string> Set;
cin >> S;
int length = S.length();
for(int l = 1; l <= length; l++){
for(int j = 0; j <= (length-l); j++){
Set.insert(S.substr(j,l));
}
}
cout << Set.size() << '\n';
return 0;
}
๐ฝ ๋ฌธ์์ด string์ substr(start, length) ํจ์๋ฅผ ํ์ฉํด ์ฃผ์ด์ง ๋ฌธ์์ด์ slicing ์งํ. start index๋ถํฐ length๊น์ง์ ๋ถ๋ถ ๋ฌธ์์ด์ด substr() ๊ฒฐ๊ณผ๋ก ๋ฆฌํด
โ 25192 ์ธ์ฌ์ฑ ๋ฐ์ ๊ณฐ๊ณฐ์ด โ
//25192
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, ans=0;
string info;
cin >> N;
set<string> S;
while(N--){
cin >> info;
if(info=="ENTER"){
ans+=S.size();
S.clear();
}
else{
S.insert(info);
}
}
cout << ans+S.size();
return 0;
}
๐ฝ ENTERํ ๋๋ง๋ค set clear๋ก ์ ์ ์ค๋ณต ์์ด ๋ฃ๊ณ ๊ธธ์ด ๋์ ๋ํจ
'C, C++ > ๐ฅ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(C++) โ Stack & Queue & Deque Intermediate I - 4 Solvedโ (0) | 2024.11.15 |
---|---|
(C++) โ Number Theory Intermediate I - 2 Solvedโ (0) | 2024.11.15 |
(C++) โ Sorting Intermediate I - 5 Solvedโ (0) | 2024.11.14 |
(C++)โ BF Intermediate I - 1 Solvedโ (0) | 2024.11.14 |
(C++) โ Implementation&Simulation Intermediate I - 2 Solvedโ (0) | 2024.11.14 |
๋๊ธ