Problem A 풀이 (C++)

 문제 링크 : https://codeforces.com/contest/1520/problem/A

n개의  task를 차례대로 확인하면서 

task가 바뀔 때마다 set 자료구조에 저장하고 

기존의 데이터에 새로운 task가 있는지 확인하여 있다면 NO 출력.

n개의 task 모두 확인이 끝났다면 YES 출력.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <set>
 
using namespace std;
 
int main() {
 
    int t;
    cin >> t;
 
    while (t--) {
 
        int n;
        cin >> n;
 
        string str;
        cin >> str;
 
        set<char> s;
 
        char before = '?';
 
        for (int i = 0; i < str.length(); i++) {
            
            if (before != str[i]) {
                if (s.count(str[i]) > 0) {
                    cout << "NO" << endl;
                    break;
                }
                s.insert(before);
            }
 
            before = str[i];
            n--;
        }
 
        if (n == 0)
            cout << "YES" << endl;
    }
 
    return 0;
}
cs

댓글