CSES - Aalto Competitive Programming 2024 - wk9 - Wed - Results
Submission details
Task:Bracket sequence
Sender:aalto2024j_001
Submission time:2024-11-06 17:15:36 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.00 sdetails
#38ACCEPTED0.00 sdetails
#39ACCEPTED0.00 sdetails
#40ACCEPTED0.00 sdetails
#41ACCEPTED0.00 sdetails
#42ACCEPTED0.00 sdetails
#43ACCEPTED0.00 sdetails
#44ACCEPTED0.00 sdetails
#45ACCEPTED0.00 sdetails
#46ACCEPTED0.00 sdetails
#47ACCEPTED0.00 sdetails
#48ACCEPTED0.00 sdetails
#49ACCEPTED0.00 sdetails
#50ACCEPTED0.00 sdetails
#51ACCEPTED0.00 sdetails
#52ACCEPTED0.00 sdetails
#53ACCEPTED0.00 sdetails
#54ACCEPTED0.00 sdetails
#55ACCEPTED0.00 sdetails
#56ACCEPTED0.01 sdetails
#57ACCEPTED0.01 sdetails
#58ACCEPTED0.01 sdetails
#59ACCEPTED0.01 sdetails
#60ACCEPTED0.01 sdetails
#61ACCEPTED0.01 sdetails
#62ACCEPTED0.01 sdetails
#63ACCEPTED0.01 sdetails
#64ACCEPTED0.01 sdetails
#65ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << ": " << x << endl;
#define ll long long
#define ull unsigned long long
#define vi vector<int>


bool check_valid_pattern(const string &s) {
    int count = 0;
    for (char c : s) {
        if (c == '(') {
            count++;
        }

        else { // ')'
            if (count == 0) {
                return false;
            } else {
                count--;
            }
        }
    }

    return count == 0;
}


string longest_valid_pattern(string s) {
    int count = 0;

    string result = "";

    string current = "";
    for (char c : s) {

        current += c;

        if (c == '(') {
            count++;
        }

        else { // ')'
            if (count == 0) {
                current = "";
            } else {
                count--;
            }
        }

        if (current.size() > result.size() && count == 0) result = current;
    }

    current = s.substr(count,s.size()-count);
    
    if (check_valid_pattern(current) && current.size() > result.size()) result = current;

    return result;
}

string longestRegularSubstring(const string& s) {
    int n = s.size();
    vector<int> dp(n, 0); // dp[i] will store the length of the longest valid substring ending at index i
    int maxLength = 0;
    int endIndex = -1;

    for (int i = 1; i < n; ++i) {
        if (s[i] == ')') {
            int j = i - dp[i - 1] - 1; // index of the matching '(' for the current ')'
            if (j >= 0 && s[j] == '(') {
                dp[i] = dp[i - 1] + 2;
                if (j > 0) {
                    dp[i] += dp[j - 1];
                }
                if (dp[i] > maxLength) {
                    maxLength = dp[i];
                    endIndex = i;
                }
            }
        }
    }

    if (maxLength == 0) {
        return "-1";
    } else {
        return s.substr(endIndex - maxLength + 1, maxLength);
    }
}

int main() {
    string s;
    cin >> s;
    cout << longestRegularSubstring(s) << endl;
    return 0;
}













// string reverse_par(string &s) {
//     string r = "";
//     for (auto it = s.rbegin(); it != s.rend(); it++) {
//         if (*it == '(') r += ')';
//         else r += '(';
//     }
//     return r;
// }


// int main() {
//     string s;
//     cin >> s;

//     string r1 = longest_valid_pattern(s);
//     // string r2 = longest_valid_pattern(reverse_par(s));


//     if (r1.size() > 0) cout << r1 << endl;
//     // else if (r2.size() > 0) cout << reverse_par(r2) << endl;

//     else cout << -1 << endl;

//     return 0;
// }

Test details

Test 1

Verdict: ACCEPTED

input
(

correct output
-1

user output
-1

Test 2

Verdict: ACCEPTED

input
))

correct output
-1

user output
-1

Test 3

Verdict: ACCEPTED

input
((

correct output
-1

user output
-1

Test 4

Verdict: ACCEPTED

input
()

correct output
()

user output
()

Test 5

Verdict: ACCEPTED

input
(()

correct output
()

user output
()

Test 6

Verdict: ACCEPTED

input
()()

correct output
()()

user output
()()

Test 7

Verdict: ACCEPTED

input
))))

correct output
-1

user output
-1

Test 8

Verdict: ACCEPTED

input
())(

correct output
()

user output
()

Test 9

Verdict: ACCEPTED

input
(()(

correct output
()

user output
()

Test 10

Verdict: ACCEPTED

input
)))))

correct output
-1

user output
-1

Test 11

Verdict: ACCEPTED

input
())(()

correct output
()

user output
()

Test 12

Verdict: ACCEPTED

input
))(()(

correct output
()

user output
()

Test 13

Verdict: ACCEPTED

input
()(())

correct output
()(())

user output
()(())

Test 14

Verdict: ACCEPTED

input
()(())

correct output
()(())

user output
()(())

Test 15

Verdict: ACCEPTED

input
()((((

correct output
()

user output
()

Test 16

Verdict: ACCEPTED

input
(())(())((

correct output
(())(())

user output
(())(())

Test 17

Verdict: ACCEPTED

input
)))((()(((

correct output
()

user output
()

Test 18

Verdict: ACCEPTED

input
(()))(((((

correct output
(())

user output
(())

Test 19

Verdict: ACCEPTED

input
())(()())(

correct output
(()())

user output
(()())

Test 20

Verdict: ACCEPTED

input
))))))))))

correct output
-1

user output
-1

Test 21

Verdict: ACCEPTED

input
())(())(()

correct output
(())

user output
(())

Test 22

Verdict: ACCEPTED

input
)((())(()(

correct output
(())

user output
(())

Test 23

Verdict: ACCEPTED

input
()(())()()

correct output
()(())()()

user output
()(())()()

Test 24

Verdict: ACCEPTED

input
()(())((()

correct output
()(())

user output
()(())

Test 25

Verdict: ACCEPTED

input
()((((((((

correct output
()

user output
()

Test 26

Verdict: ACCEPTED

input
((((((())))))))(((((((((()))))...

correct output
(((((())))))(((((((((()))))(((...

user output
(((((())))))(((((((((()))))(((...

Test 27

Verdict: ACCEPTED

input
)))((()((((((()()))(())(()(())...

correct output
((()((((((()()))(())(()(()))((...

user output
((()((((((()()))(())(()(()))((...

Test 28

Verdict: ACCEPTED

input
(()))((((((())(((()))))(())())...

correct output
(((())((()()(((((())()))())))(...

user output
(((())((()()(((((())()))())))(...

Test 29

Verdict: ACCEPTED

input
())))))))(((((((((())))(()))))...

correct output
(((((((((())))(())))))((((((()...

user output
(((((((((())))(())))))((((((()...

Test 30

Verdict: ACCEPTED

input
))))))))))))))))))))))))))))))...

correct output
-1

user output
-1

Test 31

Verdict: ACCEPTED

input
())(())(()()()(()()()))((())((...

correct output
(())(()()()(()()()))((())(((((...

user output
(())(()()()(()()()))((())(((((...

Test 32

Verdict: ACCEPTED

input
(((((((((()))))))))((((((((())...

correct output
((((((((()))))))))

user output
((((((((()))))))))

Test 33

Verdict: ACCEPTED

input
()(())()()()(((((())))(((()())...

correct output
((((())))(((()()))((())(())())...

user output
((((())))(((()()))((())(())())...

Test 34

Verdict: ACCEPTED

input
()(())((()))(((())))((((()))))...

correct output
()(())((()))(((())))((((()))))...

user output
()(())((()))(((())))((((()))))...

Test 35

Verdict: ACCEPTED

input
()(((((((()(((((((((()())())))...

correct output
()(((((((()(((((((((()())())))...

user output
()(((((((()(((((((((()())())))...

Test 36

Verdict: ACCEPTED

input
((((((((((((())))))))))))))))(...

correct output
((((((((((((((()))))))))))))((...

user output
((((((((((((((()))))))))))))((...

Test 37

Verdict: ACCEPTED

input
)))((()((((((()()))(())(()(())...

correct output
((()((((()(()((()(())()((()())...

user output
((()((((()(()((()(())()((()())...

Test 38

Verdict: ACCEPTED

input
(()))((((((())(((()))))(())())...

correct output
((((((())(((()))))(())())())((...

user output
((((((())(((()))))(())())())((...

Test 39

Verdict: ACCEPTED

input
(()))))))))))))))(((((((((((((...

correct output
(((((((((((((((((()))))))((())...

user output
(((((((((((((((((()))))))((())...

Test 40

Verdict: ACCEPTED

input
))))))))))))))))))))))))))))))...

correct output
-1

user output
-1

Test 41

Verdict: ACCEPTED

input
())(())(()()()(()()()))((())((...

correct output
(())(()()()(()()()))((())(((((...

user output
(())(()()()(()()()))((())(((((...

Test 42

Verdict: ACCEPTED

input
))))(((((((((((((())))))))))))...

correct output
((((((((((((()))))))))))))

user output
((((((((((((()))))))))))))

Test 43

Verdict: ACCEPTED

input
()(())()()()(((((())))(((()())...

correct output
()()(((()())(()(((()(()(()(()(...

user output
()()(((()())(()(((()(()(()(()(...

Test 44

Verdict: ACCEPTED

input
()(())((()))(((())))((((()))))...

correct output
()(())((()))(((())))((((()))))...

user output
()(())((()))(((())))((((()))))...

Test 45

Verdict: ACCEPTED

input
()(((((((()(((((((((()())())))...

correct output
()(((((((()(((((((((()())())))...

user output
()(((((((()(((((((((()())())))...

Test 46

Verdict: ACCEPTED

input
((((((((((((((((((((((((((((((...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 47

Verdict: ACCEPTED

input
)))((()((((((()()))(())(()(())...

correct output
(((())()((()))()())((((((()(()...

user output
(((())()((()))()())((((((()(()...

Test 48

Verdict: ACCEPTED

input
(()))((((((())(((()))))(())())...

correct output
((()))(()()((()()((((()()()(()...

user output
((()))(()()((()()((((()()()(()...

Test 49

Verdict: ACCEPTED

input
(((((((())))))))))))))))))))))...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 50

Verdict: ACCEPTED

input
))))))))))))))))))))))))))))))...

correct output
-1

user output
-1

Test 51

Verdict: ACCEPTED

input
())(())(()()()(()()()))((())((...

correct output
(((()((((((()(((((()(((()(((()...

user output
(((()((((((()(((((()(((()(((()...

Test 52

Verdict: ACCEPTED

input
(((((((())))))))))))))))))))))...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 53

Verdict: ACCEPTED

input
()(())()()()(((((())))(((()())...

correct output
()(())()()()(((((())))(((()())...

user output
()(())()()()(((((())))(((()())...

Test 54

Verdict: ACCEPTED

input
()(())((()))(((())))((((()))))...

correct output
()(())((()))(((())))((((()))))...

user output
()(())((()))(((())))((((()))))...

Test 55

Verdict: ACCEPTED

input
()(((((((()(((((((((()())())))...

correct output
(((())((())(())(((()(()()(()()...

user output
(((())((())(())(((()(()()(()()...

Test 56

Verdict: ACCEPTED

input
((((((((((((((((((((((((((((((...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 57

Verdict: ACCEPTED

input
)))((()((((((()()))(())(()(())...

correct output
(((()))(())(())(((((()())()(()...

user output
(((()))(())(())(((((()())()(()...

Test 58

Verdict: ACCEPTED

input
(()))((((((())(((()))))(())())...

correct output
(()()(()((()(((()(((()()()()((...

user output
(()()(()((()(((()(((()()()()((...

Test 59

Verdict: ACCEPTED

input
((((((((((((((((((((((((((((((...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 60

Verdict: ACCEPTED

input
))))))))))))))))))))))))))))))...

correct output
-1

user output
-1

Test 61

Verdict: ACCEPTED

input
())(())(()()()(()()()))((())((...

correct output
(()(()()()(()())(())((((((()((...

user output
(()(()()()(()())(())((((((()((...

Test 62

Verdict: ACCEPTED

input
))))))))))))))))))))))))))))))...

correct output
((((((((((((((((((((((((((((((...

user output
((((((((((((((((((((((((((((((...

Test 63

Verdict: ACCEPTED

input
()(())()()()(((((())))(((()())...

correct output
(()()((((())()()())())))((())(...

user output
(()()((((())()()())())))((())(...

Test 64

Verdict: ACCEPTED

input
()(())((()))(((())))((((()))))...

correct output
()(())((()))(((())))((((()))))...

user output
()(())((()))(((())))((((()))))...

Test 65

Verdict: ACCEPTED

input
()(((((((()(((((((((()())())))...

correct output
()(((((()()))()))()(())(((()((...

user output
()(((((()()))()))()(())(((()((...