๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1181๋ฒˆ - ๋‹จ์–ด์ •๋ ฌ

by dkswnkk 2021. 10. 31.
 

1181๋ฒˆ: ๋‹จ์–ด ์ •๋ ฌ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 20,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 50์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

ํ’€์ด1

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;
vector<string>v;
bool desc(string a,string b) {
    if (a.length() == b.length()) return a < b;    //๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌ
    else return a.length() < b.length();        //๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ์งง์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ •๋ ฌ
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T; cin >> T;

    while (T--) {
        string s; cin >> s;
        if (!(find(v.begin(), v.end(), s) != v.end())) v.push_back(s);    //๋ฐฐ์—ด์— ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ์—†์„๋•Œ ์‚ฝ์ž….
    }

    sort(v.begin(), v.end(),desc);    //์ •ํ•ด์ค€ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

    for (string s : v) {
        cout << s << "\n";
    }
}

ํ’€์ด2

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;
vector<string>v;
bool desc(string a, string b) {
    if (a.length() == b.length()) return a < b;    //๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌ
    else return a.length() < b.length();        //๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ์งง์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ •๋ ฌ
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T; cin >> T;

    while (T--) {
        string s; cin >> s;
        v.push_back(s);    //๋ฐฐ์—ด์— ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ์—†์„๋•Œ ์‚ฝ์ž….
    }

    sort(v.begin(), v.end(), desc);    //์ •ํ•ด์ค€ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

    for (int i = 0; i < v.size(); i++) {
        if (i == v.size() - 1) {
            if (v[i] != v[v.size() - 2]) {
                cout << v[i];
                return 0;
            }
        }
        if (v[i] == v[i+1]) continue;
        else cout << v[i] << "\n";
    }

}

๋Œ“๊ธ€