Skip to content

Commit b1abbec

Browse files
committed
Lv2_괄호변환
Lv2_뉴스클러스터링
1 parent 1fb6f9b commit b1abbec

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
#include <stack>
5+
using namespace std;
6+
7+
bool isOk(string v) {
8+
stack<char> s;
9+
for (auto a : v) {
10+
if (s.size() > 0) {
11+
if (s.top() == '(' && a == ')')
12+
s.pop();
13+
else
14+
s.push(a);
15+
}
16+
else
17+
s.push(a);
18+
}
19+
20+
if (s.size() != 0)
21+
return false;
22+
else
23+
return true;
24+
}
25+
26+
string solution(string p) {
27+
string answer = "";
28+
// 1. 입력이 빈 문자열인 경우, 빈 문자열을 반환합니다.
29+
if (p == "")
30+
return p;
31+
32+
// 2. 문자열 w를 두 "균형잡힌 괄호 문자열" u, v로 분리합니다. 단, u는 "균형잡힌 괄호 문자열"로 더 이상 분리할 수 없어야 하며, v는 빈 문자열이 될 수 있습니다.
33+
string u = "";
34+
string v = "";
35+
36+
int left = 0, right = 0;
37+
for (int i = 0; i < p.length(); ++i) {
38+
if (p[i] == '(')
39+
++left;
40+
else
41+
++right;
42+
43+
if (left == right){
44+
u = p.substr(0,i+1);
45+
v = p.substr(i+1, p.length() - i - 1);
46+
break;
47+
}
48+
}
49+
50+
if (isOk(u)) { // 3. 문자열 u가 "올바른 괄호 문자열" 이라면 문자열 v에 대해 1단계부터 다시 수행합니다.
51+
if (v.length() == 0)
52+
return u;
53+
else {
54+
answer += u; // 3 - 1. 수행한 결과 문자열을 u에 이어 붙인 후 반환합니다.
55+
answer += solution(v);
56+
}
57+
}
58+
else { // 4. 문자열 u가 "올바른 괄호 문자열"이 아니라면 아래 과정을 수행합니다.
59+
string nv = "("; // 4 - 1. 빈 문자열에 첫 번째 문자로 '('를 붙입니다.
60+
nv += solution(v); // 4 - 2. 문자열 v에 대해 1단계부터 재귀적으로 수행한 결과 문자열을 이어 붙입니다.
61+
nv += ")"; // 4 - 3. ')'를 다시 붙입니다.
62+
string nu = u.substr(1, u.length() - 2); // 4 - 4. u의 첫 번째와 마지막 문자를 제거하고, 나머지 문자열의 괄호 방향을 뒤집어서 뒤에 붙입니다.
63+
for (auto a : nu) {
64+
if (a == '(')
65+
nv += ")";
66+
else
67+
nv += "(";
68+
}
69+
answer += nv;
70+
}
71+
// 4 - 5. 생성된 문자열을 반환합니다.
72+
return answer;
73+
}
74+
75+
int main() {
76+
string p1 = "()))((()";
77+
string p2 = "(()())()";
78+
string p3 = ")(";
79+
cout << solution(p1);
80+
81+
return 0;
82+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <algorithm>
4+
#include <map>
5+
using namespace std;
6+
7+
int solution(string str1, string str2) {
8+
int answer = 0;
9+
string s1 = "", s2 = "";
10+
11+
for (int i = 0; i < str1.length(); ++i)
12+
str1[i] = tolower(str1[i]);
13+
14+
for (int i = 0; i < str2.length(); ++i)
15+
str2[i] = tolower(str2[i]);
16+
17+
map<string, int> m1, m2;
18+
for (int i = 0; i < str1.length()-1; ++i) {
19+
string s = str1.substr(i,2);
20+
if (isalpha(s[0]) && isalpha(s[1])) {
21+
if (m1.count(s) > 0)
22+
m1[s]++;
23+
else
24+
m1.insert({ s,1 });
25+
}
26+
}
27+
28+
for (int i = 0; i < str2.length()-1; ++i) {
29+
string s = str2.substr(i, 2);
30+
if (isalpha(s[0]) && isalpha(s[1])) {
31+
if (m2.count(s) > 0)
32+
m2[s]++;
33+
else
34+
m2.insert({ s,1 });
35+
}
36+
}
37+
38+
float f1 = 0, f2 = 0;
39+
// 합집합 찾기
40+
for (auto a : m1)
41+
f2 += a.second;
42+
for (auto b : m2)
43+
f2 += b.second;
44+
45+
// 교집합찾기
46+
for (auto a : m1) {
47+
for (auto b : m2) {
48+
if (a.first == b.first) {
49+
f1 += min(a.second, b.second);
50+
f2 -= min(a.second, b.second);
51+
}
52+
}
53+
}
54+
55+
if (f1 == 0 && f2 == 0)
56+
return 65536;
57+
else
58+
answer = f1 / f2 * 65536;
59+
60+
return answer;
61+
}
62+
63+
int main() {
64+
cout << solution("FRANCE", "french");
65+
return 0;
66+
}

Programmers/Programmers.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,18 @@
196196
<ClCompile Include="Lv2\Lv2_가장큰정사각형찾기.cpp">
197197
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
198198
</ClCompile>
199+
<ClCompile Include="Lv2\Lv2_괄호변환.cpp">
200+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
201+
</ClCompile>
199202
<ClCompile Include="Lv2\Lv2_구명보트.cpp">
200203
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
201204
</ClCompile>
202205
<ClCompile Include="Lv2\Lv2_기능개발.cpp">
203206
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
204207
</ClCompile>
208+
<ClCompile Include="Lv2\Lv2_뉴스클러스터링.cpp">
209+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
210+
</ClCompile>
205211
<ClCompile Include="Lv2\Lv2_다리를지나는트럭.cpp">
206212
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
207213
</ClCompile>

Programmers/Programmers.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,5 +525,11 @@
525525
<ClCompile Include="Lv2\Lv2_메뉴리뉴얼.cpp">
526526
<Filter>소스 파일</Filter>
527527
</ClCompile>
528+
<ClCompile Include="Lv2\Lv2_괄호변환.cpp">
529+
<Filter>소스 파일</Filter>
530+
</ClCompile>
531+
<ClCompile Include="Lv2\Lv2_뉴스클러스터링.cpp">
532+
<Filter>소스 파일</Filter>
533+
</ClCompile>
528534
</ItemGroup>
529535
</Project>

0 commit comments

Comments
 (0)