Problem
https://www.acmicpc.net/problem/5052
5052번: 전화번호 목록
첫째 줄에 테스트 케이스의 개수 t가 주어진다. (1 ≤ t ≤ 50) 각 테스트 케이스의 첫째 줄에는 전화번호의 수 n이 주어진다. (1 ≤ n ≤ 10000) 다음 n개의 줄에는 목록에 포함되어 있는 전화번호가
www.acmicpc.net
Solve
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class sol
{
vector<string> v;
int cnt;
bool check_exist(const string& a, const string& b)
{
string st = b.substr(0, a.length());
if (st == a)
{
return true;
}
else
{
return false;
}
}
public:
sol(const int& n)
{
cnt = n;
v.reserve(n);
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
v.push_back(s);
}
sort(v.begin(),v.end());
}
bool solution()
{
for (int i = 1; i < v.size(); i++)
{
bool che = check_exist(v[i - 1], v[i]); // 사전순으로 정렬하면 붙어있는 2개만 비교하면됨;;
if (che)
{
return true;
}
}
return false;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n;
cin >> n;
sol S(n);
if (S.solution())
{
cout << "NO" << '\n';
}
else
{
cout << "YES" << '\n';
}
}
return 0;
}