작성일 :

문제 링크

17838번 - 커맨드

설명

문자열이 길이 7이고 정확히 두 종류의 문자로 이루어지며, AABBABB 형태를 만족하는지 판별하는 문제입니다.


접근법

길이가 7이 아니면 바로 0입니다. 길이가 7이면 첫 문자와 세 번째 문자를 A, B로 두고 A ≠ B를 확인합니다. 그리고 인덱스 0, 1, 4가 A인지, 인덱스 2, 3, 5, 6이 B인지 검사하면 됩니다.


Code

C#

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
using System;
using System.Text;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    var sb = new StringBuilder();

    for (var i = 0; i < t; i++) {
      var s = Console.ReadLine()!;
      if (s.Length != 7) {
        sb.AppendLine("0");
        continue;
      }

      var a = s[0];
      var b = s[2];
      var ok = a != b &&
               s[1] == a && s[4] == a &&
               s[3] == b && s[5] == b && s[6] == b;

      sb.AppendLine(ok ? "1" : "0");
    }

    Console.Write(sb.ToString());
  }
}

C++

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
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    string s; cin >> s;
    if ((int)s.size() != 7) {
      cout << 0 << "\n";
      continue;
    }

    char a = s[0];
    char b = s[2];
    bool ok = a != b &&
              s[1] == a && s[4] == a &&
              s[3] == b && s[5] == b && s[6] == b;

    cout << (ok ? 1 : 0) << "\n";
  }

  return 0;
}