C, C++/๐Ÿฅˆ BOJ

(C++) โ˜…Set/Map Intermediate I - 5 Solvedโ˜…

metamong 2024. 11. 14.

โ˜… 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๋กœ ์œ ์ € ์ค‘๋ณต ์—†์ด ๋„ฃ๊ณ  ๊ธธ์ด ๋ˆ„์  ๋”ํ•จ


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€