You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
794 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1000050;
char s[N], s2[N];
char st[N];
int top;
bool check(int l)
{
top = 0;
for (int i = 0; i < l; i++)
if (top && st[top - 1] == '(' && s[i] == ')')
top--;
else
st[top++] = s[i];
return top == 0;
}
int main()
{
scanf("%s", s2);
int n = strlen(s2);
memcpy(s, s2, n);
while (prev_permutation(s, s + n))
if (check(n))
{
puts(s);
break;
}
memcpy(s, s2, n);
while (next_permutation(s, s + n))
if (check(n))
{
puts(s);
break;
}
return 0;
}