답안 #874898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874898 2023-11-18T04:45:28 Z Muhammad_Aneeq 괄호 문자열 (CEOI16_match) C++17
0 / 100
0 ms 348 KB
/*
بسم الله الرحمن الرحيم
Author:
                          (:Muhammad Aneeq:)
*/
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
string ans="";
string x;
int n;
map<char,vector<int>>d;
bool check(string s)
{
	map<char,pair<int,int>>z;
	string ans=string(n,' ');
	for (int i=0;i<s.size();i++)
	{
		ans[i]=s[i];
		if (ans[i]=='(')
			z[x[i]].first++;
		else
			z[x[i]].second++;
	}
	for (int i=s.size();i<x.size();i++)
	{
		if (z[x[i]].first>=d[x[i]].size()/2)
		{
			ans[i]=')';
			z[x[i]].second++;
		}
		else
		{
			ans[i]='(';
			z[x[i]].first++;
		}
	}
	vector<char>q;
	for (int i=0;i<n;i++)
	{
		if (ans[i]==')')
		{
			if (q.size()==0)
				return 0;
			if (q.back()!=x[i])
				return 0;
			q.pop_back();
		}
		else
			q.push_back(x[i]);
	}
	return (q.size()==0);
}
inline void solve()
{
	cin>>x;
	n=x.size();
	for (int i=0;i<n;i++)
		d[x[i]].push_back(i);
	for (auto i:d)
	{
		if (i.second.size()%2)
		{
			cout<<-1<<endl;return;
		}
		int z=0;
		for (auto j:i.second)
		{
			if (j%2)
				z++;
			else
				z--;
		}
		if (z)
		{
			cout<<-1<<endl;
			return;
		}
	}
	string ans="";
	for (int i=0;i<n;i++)
	{

		if (check(ans+'('))
			ans+='(';
		else
			ans+=')';
	}
	cout<<ans<<endl;
}
int main()
{
  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
        solve();
}

Compilation message

match.cpp: In function 'bool check(std::string)':
match.cpp:22:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |  for (int i=0;i<s.size();i++)
      |               ~^~~~~~~~~
match.cpp:30:23: 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=s.size();i<x.size();i++)
      |                      ~^~~~~~~~~
match.cpp:32:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |   if (z[x[i]].first>=d[x[i]].size()/2)
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -