본문으로 건너뛰기
풀이 목록으로 돌아가기

BOJ 4566 - Is the Name of This Problem

2026-03-03
BOJ
브론즈 I
cpp
원본 문제 보기
문자열
파싱

문제

BOJ 4566 - Is the Name of This Problem

문자열이 "A" A 형식(Quine)인지 판별하여 결과를 출력하는 문제

풀이

문자열에서 첫 번째와 두 번째 따옴표 사이의 부분문자열 A를 추출한 뒤, 두 번째 따옴표 이후의 나머지 문자열과 A가 동일한지 비교한다. 동일하면 Quine(A), 아니면 not a quine을 출력한다.

코드

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 
  string s;
  while (getline(cin, s) && s != "END") {
    if (s.length() < 3 || s[0] != '"') {
      cout << "not a quine" << endl;
      continue;
    }
 
    size_t second_quote = s.find('"', 1);
    if (second_quote == string::npos) {
      cout << "not a quine" << endl;
      continue;
    }
 
    string A = s.substr(1, second_quote - 1);
 
    if (second_quote + 1 >= s.length() || s[second_quote + 1] != ' ') {
      cout << "not a quine" << endl;
      continue;
    }
 
    string remainder = s.substr(second_quote + 2);
    if (remainder == A) {
      cout << "Quine(" << A << ")" << endl;
    } else {
      cout << "not a quine" << endl;
    }
  }
 
  return 0;
}

복잡도

  • 시간: O(N) (N: 문자열 길이)
  • 공간: O(N)

최근 글