#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
using namespace std;
constexpr int nmax = 1e5 + 2;
int n,v[nmax],frecv[nmax],deschis[nmax],nr;
char s[nmax];
int main()
{
cin.getline((s + 1),nmax);
int n = strlen(s + 1);
for(int i = 1; i <= n; i++)
{
int ch = s[i] - '0' + 1;
v[i] = ch;
if(frecv[ch] == 0)
{
deschis[++nr] = ch;
frecv[ch]++;
}
else
{
if(deschis[nr] == ch)
{
frecv[ch]--;
nr--;
}
else
{
cout << "-1";
return 0;
}
}
}
if(nr > 0)
{
cout << "-1";
return 0;
}
for(int i = 1; i <= n; i++)
{
int ch = v[i];
if(frecv[ch] == 0)
{
int ci = i, cnt = 0;
while(v[ci] == ch && ci <= n)
{
cnt++;
ci++;
}
ci--;
int j = 1;
for(; j <= (cnt + 1) / 2; j++)
{
cout << "(";
}
for(; j <= cnt; j++)
{
cout << ")";
}
i = ci;
frecv[ch] = cnt % 2;
}
else
{
int ci = i, cnt = 0;
while(v[ci] == ch && ci <= n)
{
cnt++;
ci++;
}
ci--;
int j = 1;
for(; j <= cnt / 2; j++)
{
cout << "(";
}
for(; j <= cnt; j++)
{
cout << ")";
}
i = ci;
frecv[ch] = (1 + cnt) % 2;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |