본문 바로가기

백준 문제풀이/Parsing

[C++] 백준 문제풀이 (Parsing) 3568번 iSharp

 

 

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

 

3568번: iSharp

입력으로 주어진 변수 선언문을 문제의 조건에 맞게 변형한 뒤, 한 줄에 하나씩 출력한다. 변수형과 변수명 사이에는 공백이 하나 있어야 한다. 출력은 입력으로 주어진 변수 선언문에서 변수가

www.acmicpc.net

 

 

#include <bits/stdc++.h>


std::vector<std::string> answers;

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

    std::string s;
    std::getline(std::cin, s);

    // 파싱을 편하게 하기 위한 조작
    s.pop_back();
    s += ", ";

    std::stringstream ss;
    ss.str(s);
    std::string common;
    ss >> common;
    std::string portion;

    while(ss >> portion){
        std::string str = common;
        // std::cout << portion << "\n";
        int idx = portion.length() - 2;

        while(true){
            if (portion[idx] == ']'){
                str += "[]";
                --idx;
            }
            else if ((portion[idx] >= 'a' && portion[idx] >= 'z') || (portion[idx] >= 'A' && portion[idx] >= 'Z')){
                break;
            }
            else {
                str += portion[idx];
            }
            --idx;
        }

        str += ' ';
        str += portion.substr(0, idx + 1);
        str += ';';
        answers.push_back(str);
    }

    for (auto str : answers){
        std::cout << str << "\n";
    }

    return 0;
}