Submission #154378

# Submission time Handle Problem Language Result Execution time Memory
154378 2019-09-21T12:16:43 Z arnold518 Match (CEOI16_match) C++14
100 / 100
22 ms 13308 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N;
char S[MAXN+10], ans[MAXN+10];
int P[MAXN+10][30];

void solve(int l, int r)
{
    if(r<l) return;
    int t=P[r][S[l]-'a'];
    ans[l]='('; ans[t]=')';
    solve(l+1, t-1);
    solve(t+1, r);
}

int main()
{
    int i, j;

    scanf("%s", S+1); N=strlen(S+1);

    vector<char> V;
    for(i=1; i<=N; i++)
    {
        if(!V.empty() && S[i]==V.back()) V.pop_back();
        else V.push_back(S[i]);
    }
    if(!V.empty()) return !printf("-1");

    for(j=0; j<26; j++) P[0][j]=1;
    for(i=1; i<=N; i++)
    {
        for(j=0; j<26; j++)
        {
            if('a'+j==S[i]) P[i][j]=i;
            else P[i][j]=P[P[i-1][S[i]-'a']-1][j];
        }
    }

    solve(1, N);
    printf("%s", ans+1);
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", S+1); N=strlen(S+1);
     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 1144 KB Output is correct
9 Correct 4 ms 1272 KB Output is correct
10 Correct 4 ms 1144 KB Output is correct
11 Correct 4 ms 1272 KB Output is correct
12 Correct 13 ms 8056 KB Output is correct
13 Correct 14 ms 8824 KB Output is correct
14 Correct 16 ms 9592 KB Output is correct
15 Correct 17 ms 11000 KB Output is correct
16 Correct 17 ms 11000 KB Output is correct
17 Correct 17 ms 11640 KB Output is correct
18 Correct 18 ms 11640 KB Output is correct
19 Correct 20 ms 12448 KB Output is correct
20 Correct 13 ms 8184 KB Output is correct
21 Correct 22 ms 13308 KB Output is correct