상세 컨텐츠

본문 제목

[백준 리뷰] 1032 명령 프롬프트

Problem Solving

by 전산거북이 2024. 1. 26. 20:05

본문

문제번호: 1032

사용 언어: C++

링크: https://www.acmicpc.net/problem/1032

 

1032번: 명령 프롬프트

첫째 줄에 파일 이름의 개수 N이 주어진다. 둘째 줄부터 N개의 줄에는 파일 이름이 주어진다. N은 50보다 작거나 같은 자연수이고 파일 이름의 길이는 모두 같고 길이는 최대 50이다. 파일이름은

www.acmicpc.net

 

전체 코드 보기

더보기
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
int num;

cin >> num;
string answer;

vector<string> command(num);

for (int i = 0; i < num; i++)
    cin >> command[i];

int len = command[0].length();

for (int i = 0; i < len; i++)
{
    bool question = false;
    for (int j = 1; j < num; j++)
    {
        if (command[0][i] == command[j][i])
            continue;
        else
        {
            question = true;
            break;
        }
    }

    if (question)
        answer += '?';
    else
        answer += command[0][i];
    }

    cout << answer << endl;

    return 0;
}

 

설명

1. 우선 몇 개의 문장이 입력되는지 확인합니다.

int num; cin >> num;

 

 

2. 이후, 앞으로 입력될 문장이 모두 들어갈 수 있는 vector를 생성해줍니다.

vector<string> command(num);

 

3. 모든 문장을 입력받습니다.

for (int i = 0; i < num; i++) cin >> command[i];

 

4. 모든 문장의 길이가 똑같으므로 기준은 맨 앞 문장이 됩니다.

int len = command[0].length();

 

5. 모든 문장에 대해 0번 문장과 비교하여, 다른 문자가 있는지 확인합니다.

for (int j = 1; j < num; j++) { if (command[0][i] == command[j][i]) continue; else { question = true; break; } }

 

6. 다른 문자가 있으면 ?를 넣어주고, 없으면 해당 문자를 그대로 넣어줍니다.

if (question) answer += '?'; else answer += command[0][i];

 

7. 출력하면 끝!

cout << answer << endl;

관련글 더보기

댓글 영역