#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

int cnt; //단어 종류

struct word {
    char s[15]; //단어
    int l; //단어 길이
    int ex; //출현 횟수
};

int n, m;
word S[100010];

bool CMP(word a, word b) {
    return strcmp(a.s, b.s)<0;
}

bool cmp(word a, word b) {
    if(a.ex!=b.ex) return a.ex>b.ex;
    if(a.l!=b.l) return a.l>b.l;
    return strcmp(a.s, b.s)<0;
}

int binary_search(char a[15]) {
    int s=0, e=cnt;

    while(s<=e) {
        int m=(s+e)/2;
        
        if(strcmp(a, S[m].s)==0) {
            S[m].ex++;
            return 0;
        }
        else if(strcmp(a, S[m].s)<0) {
            e=m-1;
        }
        else {
            s=m+1;
        }
    }

    return 1;
}

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

    cin >> n >> m;

    for(int i=0; i<n; i++) {
        int exist=1; //단어장에 이미 등록된 경우(0), 신규(1)
        char a[15];
        cin >> a;
        
        exist=binary_search(a);
        
        if(exist) {
            strcpy(S[cnt].s, a);
            S[cnt].l=strlen(a);
            cnt++;
        }

        sort(S, S+cnt, CMP);
    }

    sort(S, S+cnt, cmp);

    for(int i=0; i<cnt; i++) {
        if(S[i].l>=m) {
            cout << S[i].s << '\n';
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: