# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
745452 | alexdumitru | Match (CEOI16_match) | C++14 | 1 ms | 468 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |