Submission #609254

# Submission time Handle Problem Language Result Execution time Memory
609254 2022-07-27T13:02:16 Z Icebear16 Match (CEOI16_match) C++14
10 / 100
1 ms 212 KB
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define INF 1e18//change to const int INF=1e18 if causing problem
const ll MOD=998244353;
const ll alt=1e10;
const ll inf=1e9+7;//Precalc is not a bad idea
//#define int ll
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define mod(a) (a+inf)%inf
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(a) a.size()

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	string s,c="";
	cin>>s;
	vector<int> v[26];
	vector<int> p(26,0);
	vector<int> cnt(26,0);
	vector<int> a(sz(s),0);
	for(int i=0;i<sz(s);i++){
		a[i]=cnt[int(s[i])-'a']+1;
		cnt[int(s[i])-'a']+=1;
		v[int(s[i])-'a'].pb(i);
		c+='0';
	}
//	vector<int> m(sz(s),0);
	for(int i=0;i<sz(s);i++){
		p[int(s[i]-'a')]++;
		int k=0;
		if(c[i]=='0'){
			set<int> l;
			l.clear();
			for(int j=i+1;j<sz(s);j++){
				if(s[j]!=s[i]){
					if((a[j]-p[int(s[j])-'a'])%2==0){
						l.erase(int(s[j]-'a'));
					}else{
						l.insert(int(s[j]-'a'));
					}
				}
				if(s[j]==s[i] and c[j]=='0' and (i+j)%2==1 and (a[i]+a[j])%2==1 and sz(l)==0){
					k=j;
				}
				if(c[j]==')' or j==sz(s)-1){
					c[i]='(';
					c[k]=')';
					break;
				}
			}
		}
	}
	bool flag=true;
	for(int i=0;i<sz(s);i++){
		if(c[i]=='0'){
			flag=false;
			break;
		}
	}
	if(flag==false){
		for(int i=0;i<26;i++){
			vector<int> m(sz(v[i]),0);
			int j=0;
			while(j<sz(v[i])){
				if(m[j]==0){
					for(int k=sz(v[i])-1;k>j;k--){
						if(m[k]==0 and (v[i][j]+v[i][k])%2==1 and (a[v[i][j]]+a[v[i][k]])%2==1){
							m[j]=1,m[k]=1;
							c[v[i][j]]='(';
							c[v[i][k]]=')';
							break;
						}
					}
				}
				j++;
			}
		}
		for(int i=0;i<sz(c);i++){
			if(c[i]=='0'){
				flag=false;
				break;
			}
		}
	}
//	cout<<c<<endl;
	if(c[0]=='(' and flag==true) cout<<c<<endl;
	else cout<<-1<<endl;
//	main();
	return 0;
}
//Icebear

Compilation message

match.cpp: In function 'int main()':
match.cpp:30:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  for(int i=0;i<sz(s);i++){
      |               ^
match.cpp:37:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  for(int i=0;i<sz(s);i++){
      |               ^
match.cpp:43:19: 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 j=i+1;j<sz(s);j++){
      |                   ^
match.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     if(c[j]==')' or j==sz(s)-1){
      |                     ~^~~~~~~~~
match.cpp:63:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  for(int i=0;i<sz(s);i++){
      |               ^
match.cpp:73:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |    while(j<sz(v[i])){
      |           ^
match.cpp:87:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for(int i=0;i<sz(c);i++){
      |                ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -