/*
#ifndef ONLINE_JUDGE
#include "AkbarKING.h" // aska geldi akbarking.h
#else
#define debug(...)
#define debugArr(...)
#define debugG(...)
#endif
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define int long long
#define $AzH_TxdmN$ ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#define ep emplace_back
#define pb push_back
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using __indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using __indexed_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int sz = 3e5+9;
const int LOG = 63;
const int MOD = 1e9+7;
const int INF = 1e18;
int sdp[sz];
string s;
bool check(const string &str)
{
if (count(all(str),'#') > 0)
{
return 0;
}
stack<int>st;
for (char ch : str)
{
if (ch != ')')
{
st.push(ch);
}
else
{
if (!st.empty() && (ch == ')' && st.top() == '('))
{
st.pop();
}
else
{
return 0;
}
}
}
if ((int)st.size() != 0)
{
return 0;
}
for (int i = 0; i < (int)str.size(); ++i)
{
array<int, 2> Q = {i, sdp[i]};
if (Q[0] > Q[1]) swap(Q[0],Q[1]);
++Q[0];
if (Q[0] > Q[1]) swap(Q[0],Q[1]);
/// cout << i << " -> " << (Q[1] - Q[0]) << "\n";
if ((Q[1] - Q[0]) & 1)
{
return 0;
}
}
return 1;
}
void solve()
{
cin>>s;
map<char, set<int> >dp;
for (int i = 0; i < (int)s.length(); ++i)
{
dp[s[i]].insert(i);
}
for (auto &x : dp)
{
if (((int)x.second.size()) & 1)
{
cout<<"-1\n";
return;
}
}
if((int)s.length() & 1)
{
cout<<"-1\n";
return;
}
string res((int)s.length(),'#');
for (int i = 0; i < (int)s.length(); ++i)
{
if (res[i] == '#')
{
sdp[i] = *dp[s[i]].rbegin();
sdp[sdp[i]] = i;
res[i] = '(';
res[sdp[i]] = ')';
dp[s[i]].erase(*dp[s[i]].rbegin());
dp[s[i]].erase(*dp[s[i]].begin());
}
}
cout<<(check(res) ? res : "-1")<<'\n';
}
signed main()
{
$AzH_TxdmN$
freopen("match.in", "r", stdin);
freopen("match.out", "w", stdout);
int t = 1;
//cin>>t;
while (t--)
{
solve();
}
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:128:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen("match.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:129:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen("match.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |