Submission #745446

# Submission time Handle Problem Language Result Execution time Memory
745446 2023-05-20T06:55:57 Z alexdumitru Match (CEOI16_match) C++14
0 / 100
0 ms 468 KB
#include <bits/stdc++.h>

using namespace std;

ifstream fin("match.in");
ofstream fout("match.out");

const int NMAX = 100005;

string s;
int n;

vector<int> pos[100];
vector<int> poz[2][100];

int a[NMAX];
int paritate[NMAX];

int Par[NMAX];

int sir[NMAX];

int sp[26][NMAX];

void solve()
{
    cin >> s;
    n = s.length();
    for(int i = 0; i < n; i++)
        a[i] = s[i] - 'a';
    for(int i = 0; i < n; i++)
    {
        pos[a[i]].push_back(i);
        if(pos[a[i]].size() % 2 == 1)
        {
            poz[0][a[i]].push_back(i);
            paritate[i] = 0;
        }
        else {
            poz[1][a[i]].push_back(i);
            paritate[i] = 1;
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(i > 0)
        {
            for(int j = 0; j < 26; j++)
                sp[j][i] = sp[j][i - 1];
        }
        sp[a[i]][i]++;
    }
    for(int i = 0; i < n; i++)
    {
        Par[a[i]] = 1 - Par[a[i]];
        if(sir[i] == -1)
            continue;
        int st = 0;
        int dr = poz[1 - paritate[i]][a[i]].size() - 1;
        int p = 1 - paritate[i];
        int Max = -1;
        while(st <= dr)
        {
            int mid = (st + dr) / 2;
            if(poz[p][a[i]][mid] <= i)
                st = mid + 1;
            else {
                int k = poz[p][a[i]][mid];

                bool ok = 1;
                for(int j = 0; j < 26; j++)
                {
                    //verificam pentru fiecare litera care se afla in mijloc daca se poate completa
                    if((sp[j][k - 1] - sp[j][i]) % 2 == 1)
                    {
                        ok = 0;
                        break;
                    }

                }
                if(ok)
                {
                    Max = k;
                    st = mid + 1;
                }
                else dr = mid - 1;
            }
        }
        //fout << i << ' ' << Max << '\n';
        if(Max != -1)
        {
            sir[i] = 1;
            sir[Max] = -1;
        }
        else {
            cout << -1 << '\n';
            exit(0);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(sir[i] == 1)
            cout << '(';
        else cout << ')';
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Incorrect 0 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Incorrect 0 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Incorrect 0 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -