알고리즘

[알고리즘] 백준 5052 - 전화번호 목록 (C++)

blueberrysoda 2024. 9. 12. 23:15

[문제 링크] : https://www.acmicpc.net/problem/5052

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int T, N;
    cin >> T;
    string S;

    while(T--){
        cin >> N;
        vector<string> V;
        for(int i=0; i<N; i++){
            cin >> S;
            V.push_back(S);
        }

        sort(V.begin(), V.end());
        bool f = true;

        for(int i=0; i<V.size()-1; i++){
            string cur = V[i];
            string next = V[i + 1];
            f = true;
            if(cur.length() > next.length()) continue;
            if(cur == next.substr(0, cur.length())){
                f = false;
                break;
            }
        }
        if(f == false){
            cout << "NO\n";
        }
        else{
            cout << "YES\n";
        }
    }
    return 0;
}