Submission #761772

# Submission time Handle Problem Language Result Execution time Memory
761772 2023-06-20T09:11:28 Z Tenis0206 Match (CEOI16_match) C++11
100 / 100
30 ms 62640 KB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e5;

string s;
string rez;

int l[nmax + 5][155];

bool ok = true;

void solve(int st, int dr)
{
    if(st > dr)
    {
        return;
    }
    int poz = l[dr][s[st]];
    if(poz==-1 || poz <= st)
    {
        ok = false;
        return;
    }
    rez[st] = '(';
    rez[poz] = ')';
    solve(st+1,poz-1);
    solve(poz+1,dr);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef home
    freopen("nr.in","r",stdin);
    freopen("nr.out","w",stdout);
#endif // home
    cin>>s;
    int n = s.size();
    rez.resize(n + 1);
    s = "#" + s;
    for(int c='a';c<='z'; c++)
    {
        l[0][c] = -1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int c='a';c<='z';c++)
        {
            l[i][c] = -1;
        }
        int poz = l[i - 1][s[i]];
        if(s[i - 1] == s[i])
        {
            poz = i - 1;
        }
        l[i][s[i]] = i;
        if(poz == -1)
        {
            continue;
        }
        l[i][s[poz - 1]] = max(l[i][s[poz-1]], poz - 1);
        for(int c='a';c<='z';c++)
        {
            l[i][c] = max(l[i][c], l[poz - 1][c]);
        }
    }
    solve(1,n);
    if(!ok)
    {
        cout<<-1<<'\n';
        return 0;
    }
    for(int i=1; i<=n; i++)
    {
        cout<<rez[i];
    }
    cout<<'\n';
    return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:20:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   20 |     int poz = l[dr][s[st]];
      |                          ^
match.cpp: In function 'int main()':
match.cpp:54:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   54 |         int poz = l[i - 1][s[i]];
      |                                ^
match.cpp:59:18: warning: array subscript has type 'char' [-Wchar-subscripts]
   59 |         l[i][s[i]] = i;
      |                  ^
match.cpp:64:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   64 |         l[i][s[poz - 1]] = max(l[i][s[poz-1]], poz - 1);
      |                        ^
match.cpp:64:45: warning: array subscript has type 'char' [-Wchar-subscripts]
   64 |         l[i][s[poz - 1]] = max(l[i][s[poz-1]], poz - 1);
      |                                             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 1 ms 1480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 1 ms 1480 KB Output is correct
8 Correct 2 ms 4304 KB Output is correct
9 Correct 2 ms 4564 KB Output is correct
10 Correct 2 ms 4564 KB Output is correct
11 Correct 2 ms 4692 KB Output is correct
12 Correct 18 ms 37792 KB Output is correct
13 Correct 20 ms 40956 KB Output is correct
14 Correct 22 ms 44124 KB Output is correct
15 Correct 23 ms 50708 KB Output is correct
16 Correct 23 ms 50704 KB Output is correct
17 Correct 26 ms 53732 KB Output is correct
18 Correct 26 ms 55884 KB Output is correct
19 Correct 30 ms 59304 KB Output is correct
20 Correct 19 ms 37976 KB Output is correct
21 Correct 29 ms 62640 KB Output is correct