PS

[백준] 4848번 집합 숫자 표기법 C++

Eastplanet 2022. 6. 25. 09:30

문제 출처: https://www.acmicpc.net/problem/4848

 

4848번: 집합 숫자 표기법

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 두 줄로 이루어져 있고, 집합 숫자 표기법으로 나타낸 수가 주어진다. 두 수의 합은 항상 15보다 작거나 같다.

www.acmicpc.net

아래의 식을 통해서 다음 값을 구할수 있다.

string next = prev;

next[next.size()-1] = ',';

next += prev;

next += "}"; 

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include <string>

using namespace std;

string arr[16];

string join(string a) {
    string temp = a;
    temp[temp.size() - 1] = ',';
    temp += a;
    temp += "}";
    return temp;
}

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

    arr[0] = "{}";
    arr[1] = "{{}}";
    
    for (int i = 2; i < 16; i++) {
        arr[i] = join(arr[i - 1]);
    }

    int T;
    cin >> T;
    while (T--) {
        string a, b;
        cin >> a >> b;
        int anum, bnum;
        for (int i = 0; i < 16; i++) {
            if (arr[i] == a)anum = i;
            if (arr[i] == b)bnum = i;
        }
        cout << arr[anum + bnum] << '\n';
    }
    
}