BOJ1013: Contact

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

  • 간단한 구현문제. 포스팅 초안을 자동으로 써주는 스크립트를 짰는데 매우 편함.. 진작에 왜 안짰을까.
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
#include <bits/stdc++.h>
using namespace std;

bool check(string s) {
  int i = 0;
  if(i == s.size() || s[i] != '0') return false;
  while(i < s.size() && s[i] == '0') i++;
  if(i == s.size() || s[i] != '1') return false;
  while(i < s.size() && s[i] == '1') i++;
  return i == s.size();
}


bool go(string s) {
  if (s.size() == 0) return true;
  if(s[0] == '0') {
    if (s[1] != '1') return false;
    return go(s.substr(2));
  } else {
    if (s[1] != '0') return false;
    for(int i = 2; i <= s.size()-2; i++)
      if(check(s.substr(2, i)))
        if (go(s.substr(2+i)))
          return true;
    return false;
  }
}

int main() {
  int TC;
  cin >> TC;
  while(TC--) {
    string s;
    cin >> s;
    cout << (go(s) ? "YES\n" : "NO\n");
  }
}