알고리즘
백준 5052번 전화번호 목록 C++
영춘권의달인
2022. 8. 14. 10:45
우선 처음에 입력받은 전화번호들을 전부 set에 넣고 전화번호를 앞에서부터 하나씩 탐색하면서 set에 있는 번호와 같은 번호가 나오면 일관성이 없는 경우이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
bool ans = true;
int n;
cin >> n;
set<string> st;
vector<string> strs;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
strs.push_back(str);
st.insert(str);
}
for (int i = 0; i < n; i++) {
string tmp = "";
for (int j = 0; j < strs[i].length() - 1; j++) {
tmp += strs[i][j];
if (st.find(tmp) != st.end()) {
ans = false;
break;
}
}
if (ans == false) break;
}
if (ans) cout << "YES";
else cout << "NO";
cout << "\n";
}
}