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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int nax=5e4+5;
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
optimise;
string t;
cin>>t;
if(t.size()%2){
cout <<-1<<endl;
return 0;
}
int cnt[26];
memset(cnt,0,sizeof cnt);
for (int i = 0; i < t.size(); ++i)
{
cnt[(int)(t[i]-'a')]++;
}
bool test=true;
for (int i = 0; i < 26; ++i)
{
if(cnt[i]%2) test=false;
}
if(!test){
cout <<-1<<endl;
}
int cur[26];
stack<int> lst[26];
memset(cur,0,sizeof cur);
string ans = "";
for (int i = 0; i < t.size(); ++i)
{
if(lst[t[i]-'a'].empty()||(i-lst[t[i]-'a'].top())%2==0||cur[t[i]-'a']<=cnt[t[i]-'a']/2){
cur[t[i]-'a']++;
cnt[t[i]-'a']--;
ans.pb('(');
lst[t[i]-'a'].push(i);
}else{
cur[t[i]-'a']--;
cnt[t[i]-'a']--;
ans.pb(')');
lst[t[i]-'a'].pop();
}
}
int a=0;
for (int i = 0; i < ans.size(); ++i)
{
a+=(ans[i]=='(' ? 1 : -1);
}
if(a!=0) cout <<-1<<endl;
else cout <<ans<<endl;
}
Compilation message (stderr)
match.cpp: In function 'int main()':
match.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < t.size(); ++i)
| ~~^~~~~~~~~~
match.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < t.size(); ++i)
| ~~^~~~~~~~~~
match.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < ans.size(); ++i)
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |