Submission #231422

# Submission time Handle Problem Language Result Execution time Memory
231422 2020-05-13T14:45:17 Z nicolaalexandra Match (CEOI16_match) C++14
100 / 100
1407 ms 2048 KB
#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
char v[DIM],sol[DIM],s[DIM];
int n,i;

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, k = 0;

    for (i=dr;i>st;i--){
        if (!k && v[i] == v[st])
            break;
        if (k && s[k] == v[i])
            k--;
        else s[++k] = 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
    int k = 0;
    s[++k] = v[1];
    for (i=2;i<=n;i++){
        if (k && s[k] == v[i])
            k--;
        else s[++k] = v[i];
    }

    if (k){
        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:18:9: warning: unused variable 'val' [-Wunused-variable]
     int val = v[st] - 'a';
         ^~~
match.cpp: In function 'int main()':
match.cpp:39:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin>>v+1;
          ~^~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 9 ms 384 KB Output is correct
10 Correct 7 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 55 ms 1272 KB Output is correct
13 Correct 28 ms 1280 KB Output is correct
14 Correct 373 ms 1632 KB Output is correct
15 Correct 13 ms 2048 KB Output is correct
16 Correct 12 ms 2048 KB Output is correct
17 Correct 57 ms 2040 KB Output is correct
18 Correct 17 ms 1280 KB Output is correct
19 Correct 614 ms 1516 KB Output is correct
20 Correct 238 ms 1400 KB Output is correct
21 Correct 1407 ms 1972 KB Output is correct