Submission #745452

# Submission time Handle Problem Language Result Execution time Memory
745452 2023-05-20T07:09:52 Z alexdumitru Match (CEOI16_match) C++14
0 / 100
1 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;
        for(int mid = 0; mid < poz[1 - paritate[i]][a[i]].size(); mid++)
        {
            if(poz[p][a[i]][mid] <= i)
                continue;
            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;
                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;
}

Compilation message

match.cpp: In function 'void solve()':
match.cpp:62:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for(int mid = 0; mid < poz[1 - paritate[i]][a[i]].size(); mid++)
      |                          ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:58:13: warning: unused variable 'st' [-Wunused-variable]
   58 |         int st = 0;
      |             ^~
match.cpp:59:13: warning: variable 'dr' set but not used [-Wunused-but-set-variable]
   59 |         int dr = poz[1 - paritate[i]][a[i]].size() - 1;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 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 1 ms 468 KB Output is correct
2 Incorrect 0 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -