-
Notifications
You must be signed in to change notification settings - Fork 1
/
튜플.cpp
101 lines (81 loc) · 2.12 KB
/
튜플.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 튜플 (문자열)
// https://programmers.co.kr/learn/courses/30/lessons/64065
// 시행착오
// 1. 문자열의 원소집합들이 개수 순서대로 정렬되어 있어야 현재집합 - 이전집합 연산이 가능하다.
#include <string>
#include <vector>
#include <iostream>
#include <stack>
#include <sstream>
#include <algorithm>
#include "Print.h"
using namespace std;
static std::vector<std::string> Split(const std::string& str, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream token_stream(str);
while (std::getline(token_stream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
std::vector<std::string> SplitSets(const std::string& str)
{
vector<string> splits;
string temp = "";
bool open = false;
for (auto c : str)
{
if (c == '{')
{
temp = "";
open = true;
}
else if (c == '}')
{
splits.push_back(temp);
open = false;
}
else
{
if(open)
temp.push_back(c);
}
}
return splits;
}
vector<int> solution(string s)
{
vector<int> answer;
string setstr = s.substr(1, s.size() - 2);
vector<string> sets = SplitSets(setstr);
vector<vector<string>> all;
for (auto str : sets)
{
vector<string> elems = Split(str, ',');
all.push_back(elems);
}
sort(begin(all), end(all), [](const vector<string>& a, const vector<string>& b) { return a.size() < b.size(); });
vector<string> prev_elems;
for (auto elems : all)
{
vector<string> output;
sort(begin(elems), end(elems));
set_difference(begin(elems), end(elems), begin(prev_elems), end(prev_elems), back_inserter(output));
answer.push_back(stoi(output[0]));
prev_elems = elems;
}
return answer;
}
int main()
{
//string s = "{{2},{2,1},{2,1,3},{2,1,3,4}}";
string s = "{{1,2,3},{2,1},{1,2,4,3},{2}}";
//string s = "{{123}}";
vector<int> answer = solution(s);
Print::Container(answer);
return 0;
}