This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <vector>
#include <stack>
#include <string.h>
using namespace std;
const int MAXN = 2e3 + 5, MAXA = 30;
int N, p[MAXA][MAXN];
char s[MAXN], res[MAXN];
bool valid(int i, int j){
stack<char> st;
for(int k=i; k<=j; ++k){
if(st.empty()) st.push(s[k]);
else if(st.top() == s[k]) st.pop();
else st.push(s[k]);
}
return st.empty();
}
int n(char c){ return c - 'a'; }
void solve(){
for(int i=N-1; i>=0; --i){
p[n(s[i])][i] = i;
for(int j=i-1; j>=0; --j)
if(p[n(s[j])][i] == -1 && valid(j + 1, i)) p[n(s[j])][i] = j;
}
}
void Fill(int i, int j){
if(i > j) return;
int k = p[n(s[i])][j];
res[i] = '(', res[k] = ')';
Fill(i + 1, k - 1); Fill(k + 1, j);
}
int main(){
scanf("%s", s);
N = strlen(s);
if(!valid(0, N - 1)){ puts("-1"); return 0; }
memset(p, -1, sizeof(p));
solve();
Fill(0, N - 1);
puts(res);
return 0;
}
Compilation message (stderr)
match.cpp: In function 'int main()':
match.cpp:41:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |