Submission #51027

# Submission time Handle Problem Language Result Execution time Memory
51027 2018-06-15T16:22:42 Z SpaimaCarpatilor Match (CEOI16_match) C++17
100 / 100
20 ms 13536 KB
#include<bits/stdc++.h>

using namespace std;

int N, t[100009], prv[100009][26];
char sir[100009], ans[100009];

void solve (int i, int j)
{
    if (i > j) return ;
    int k = (sir[j] == sir[i] ? j : prv[j][sir[i]]);
    ans[i] = '(', ans[k] = ')';
/*    for (int k=1; k<=N; k++)
        if (ans[k] == 0) printf (".");
        else printf ("%c", ans[k]);
    printf ("\n");*/
    solve (i + 1, k - 1);
    solve (k + 1, j);
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%s", sir + 1), N = strlen (sir + 1);
for (int i=1; i<=N; i++)
    sir[i] -= 'a';
for (int i=1; i<=N; i++)
{
    if (i > 1 && sir[i - 1] == sir[i]) t[i] = i - 1;
    else t[i] = prv[i - 1][sir[i]];
    if (t[i] != 0)
    {
        int forbC = sir[t[i] - 1];
        if (t[i] == 1) forbC = -1;
        else prv[i][forbC] = t[i] - 1;
        for (int c=0; c<26; c++)
            if (c != forbC)
                prv[i][c] = prv[t[i] - 1][c];
    }
}
int maybe = t[N];
while (maybe > 1 && maybe != 0)
    maybe = t[maybe - 1];
if (maybe != 1)
{
    printf ("-1\n");
    return 0;
}
solve (1, N);
printf ("%s\n", ans + 1);
return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:11:50: warning: array subscript has type 'char' [-Wchar-subscripts]
     int k = (sir[j] == sir[i] ? j : prv[j][sir[i]]);
                                                  ^
match.cpp: In function 'int main()':
match.cpp:32:34: warning: array subscript has type 'char' [-Wchar-subscripts]
     else t[i] = prv[i - 1][sir[i]];
                                  ^
match.cpp:26:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%s", sir + 1), N = strlen (sir + 1);
 ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 672 KB Output is correct
6 Correct 3 ms 688 KB Output is correct
7 Correct 3 ms 816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 672 KB Output is correct
6 Correct 3 ms 688 KB Output is correct
7 Correct 3 ms 816 KB Output is correct
8 Correct 3 ms 1248 KB Output is correct
9 Correct 3 ms 1504 KB Output is correct
10 Correct 3 ms 1504 KB Output is correct
11 Correct 3 ms 1628 KB Output is correct
12 Correct 10 ms 7924 KB Output is correct
13 Correct 11 ms 8496 KB Output is correct
14 Correct 11 ms 9616 KB Output is correct
15 Correct 12 ms 11172 KB Output is correct
16 Correct 14 ms 11324 KB Output is correct
17 Correct 13 ms 11872 KB Output is correct
18 Correct 15 ms 11872 KB Output is correct
19 Correct 15 ms 12648 KB Output is correct
20 Correct 11 ms 12648 KB Output is correct
21 Correct 20 ms 13536 KB Output is correct