답안 #231420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
231420 2020-05-13T14:40:27 Z nicolaalexandra 괄호 문자열 (CEOI16_match) C++14
37 / 100
2000 ms 24696 KB
#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
char v[DIM],sol[DIM];
deque <char> s,d;
int n,i;

int verif (int poz){
    for (int i=poz;i<=n;i++){
        if (!d.empty() && d.back() == v[i])
            d.pop_back();
        else d.push_back(v[i]);
    }
    return d.empty();
}


void solve (int st, int dr){
    /// in st pun ( si trb sa gasesc )
    if (st >= dr)
        return;
    if (st == dr-1){
        sol[st] = '(';
        sol[dr] = ')';
        return;
    }

    sol[st] = '(';
    int val = v[st] - 'a';
    int i;
    deque <char> s;
    for (i=dr;i>st;i--){
        if (s.empty() && v[i] == v[st])
            break;
        if (!s.empty() && s.back() == v[i])
            s.pop_back();
        else s.push_back(v[i]);
    }

    sol[i] = ')';
    solve (st+1,i-1);
    solve (i+1,dr);
}

int main (){

   //ifstream cin ("match.in");
   // ofstream cout ("match.out");

    cin>>v+1;
    n = strlen (v+1);

    /// verific daca am solutie
    s.push_back(v[1]);
    for (i=2;i<=n;i++){
        if (!s.empty() && s.back() == v[i])
            s.pop_back();
        else s.push_back(v[i]);
    }

    if (!s.empty()){
        cout<<-1;
        return 0;
    }

    /*for (i=1;i<=n;i++)
        poz[v[i]-'a'].push_back(i);

    for (i=0;i<26;i++)
        reverse (poz[i].begin(),poz[i].end());*/

    solve (1,n);

    for (i=1;i<=n;i++)
        cout<<sol[i];

    return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:29:9: warning: unused variable 'val' [-Wunused-variable]
     int val = v[st] - 'a';
         ^~~
match.cpp: In function 'int main()':
match.cpp:50:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin>>v+1;
          ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 896 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 896 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 7 ms 512 KB Output is correct
9 Correct 16 ms 1792 KB Output is correct
10 Correct 10 ms 1536 KB Output is correct
11 Correct 8 ms 2560 KB Output is correct
12 Correct 142 ms 12536 KB Output is correct
13 Correct 76 ms 13816 KB Output is correct
14 Correct 776 ms 20944 KB Output is correct
15 Correct 30 ms 24056 KB Output is correct
16 Correct 31 ms 24056 KB Output is correct
17 Correct 105 ms 24696 KB Output is correct
18 Correct 28 ms 10056 KB Output is correct
19 Correct 1174 ms 19320 KB Output is correct
20 Correct 533 ms 18040 KB Output is correct
21 Execution timed out 2089 ms 18616 KB Time limit exceeded