이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/// oare cat de bine merge in practica?
#include <bits/stdc++.h>
using namespace std;
int s[100010];
char sol[100010];
int ok;
int stk[100010];
int match (int st , int dr){
int i , elem = 0;
for (i=st;i<=dr;i++){
if (elem && stk[elem] == s[i])
elem--;
else stk[++elem] = s[i];
}
return (elem == 0);
}
void solve (int st , int dr){
int i;
if (st > dr)
return;
if (!ok) /// nu mai are rost
return;
/// stii ca pe st pui ( , gaseste i match ul
for (i = dr; i >= st + 1 ; i--){
if (match(st + 1 , i - 1) && match(i + 1 , dr))
break;
}
if (i < st + 1){
ok = 0;
return;
}
else {
sol[st] = '(';
sol[i] = ')';
solve(st + 1 , i - 1);
solve (i + 1 , dr);
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , i;
char c;
c=fgetc(fin);
n = 0;
while ( 'a' <= c && c <= 'z' ){
s[++n] = c-'a';
c=fgetc(fin);
}
ok = 1;
solve (1 , n);
if (ok == -1){
fprintf (fout,"%d\n",ok);
}
else {
for (i=1;i<=n;i++)
fputc(sol[i],fout);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |