forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonp.cpp
67 lines (67 loc) · 837 Bytes
/
onp.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
// 2008-01-29
#include <iostream>
#include <string>
using namespace std;
string convert(string e)
{
int i,depth,j;
e.erase(0,1);
e.erase(e.length()-1,1);
string l="",r="",o;
if (e[0]!='(')
{
l=e[0];
o=e[1];
}
else
{
i=0;
depth=1;
do
{
i++;
if (e[i]=='(')
depth++;
else if (e[i]==')')
depth--;
}
while (depth>0);
for (j=0; j<=i; j++)
l=l+e[j];
o=e[l.length()];
l=convert(l);
}
if (e[e.length()-1]!=')')
r=e[e.length()-1];
else
{
i=e.length()-1;
depth=1;
do
{
i--;
if (e[i]==')')
depth++;
else if (e[i]=='(')
depth--;
}
while (depth>0);
for (j=i; j<e.length(); j++)
r=r+e[j];
r=convert(r);
}
return l+r+o;
}
int main()
{
int t,i;
string s,S;
cin >> t;
for (i=0; i<t; i++)
{
cin >> s;
S=convert(s);
cout << S << endl;
}
return 0;
}