Submission #244728

# Submission time Handle Problem Language Result Execution time Memory
244728 2020-07-04T18:39:56 Z evpipis Match (CEOI16_match) C++11
37 / 100
2000 ms 880 KB
#include <bits/stdc++.h>
using namespace std;

const int len = 1e5+5;
char str[len], out[len];
stack<char> main_stack;
int n;

bool check(int i){
    stack<char> sec_stack(main_stack);
    sec_stack.push(str[i++]);

    while (i < n){
        if (sec_stack.empty() || sec_stack.top() != str[i])
            sec_stack.push(str[i]);
        else
            sec_stack.pop();

        i++;
    }

    return sec_stack.empty();
}

int main(){
    scanf("%s", str);
    n = strlen(str);

    int ans = 1;
    for (int i = 0; i < n; i++){
        int test = check(i);

        if (!test && main_stack.empty()){
            ans = 0;
            break;
        }

        //printf("i = %d, test = %d\n", i, test);

        if (test)
            main_stack.push(str[i]), out[i] = '(';
        else
            main_stack.pop(), out[i] = ')';
    }

    if (!ans){
        printf("-1\n");
        return 0;
    }

    for (int i = 0; i < n; i++)
        printf("%c", out[i]);
    printf("\n");
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", str);
     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 7 ms 256 KB Output is correct
6 Correct 9 ms 384 KB Output is correct
7 Correct 13 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 7 ms 256 KB Output is correct
6 Correct 9 ms 384 KB Output is correct
7 Correct 13 ms 256 KB Output is correct
8 Correct 104 ms 504 KB Output is correct
9 Correct 124 ms 384 KB Output is correct
10 Correct 103 ms 384 KB Output is correct
11 Correct 101 ms 388 KB Output is correct
12 Execution timed out 2067 ms 880 KB Time limit exceeded
13 Halted 0 ms 0 KB -