https://www.acmicpc.net/problem/5052

 

5052번: 전화번호 목록

첫째 줄에 테스트 케이스의 개수 t가 주어진다. (1 ≤ t ≤ 50) 각 테스트 케이스의 첫째 줄에는 전화번호의 수 n이 주어진다. (1 ≤ n ≤ 10000) 다음 n개의 줄에는 목록에 포함되어 있는 전화번호가

www.acmicpc.net

// https://www.acmicpc.net/problem/5052

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

using namespace std;

int main() {
	int t, n;
	cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> n;
		string input[10000];
		for (int j = 0; j < n; j++) {
			cin >> input[j];
		}
		
		sort(input, input + n);

		int flag = 0;
		for (int i = 0; i < n-1; i++) {
			string tmp1 = input[i];
			string tmp2 = input[i + 1];

			if (tmp1.size() > tmp2.size())
				continue;

			else if(tmp1 == tmp2.substr(0,tmp1.size())) {
				cout << "NO" << "\n";
				flag = 1;
				break;
			}
		}

		if(flag==0)
			cout << "YES" << "\n";
	}

	return 0;
}

'APS > 백준' 카테고리의 다른 글

[2252] 줄 세우기 C++  (0) 2022.04.18
[1197] 최소 스패닝 트리 C++  (0) 2022.03.15
[21924] 도시 건설 C++  (0) 2022.03.05
[14716] 현수막 C++  (0) 2022.03.05
[10816] 숫자 카드2 C++  (0) 2022.03.03

+ Recent posts