Submission #129023

# Submission time Handle Problem Language Result Execution time Memory
129023 2019-07-11T12:42:07 Z davitmarg Match (CEOI16_match) C++17
100 / 100
105 ms 17872 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <stack>
#include <cassert>
#include <iterator>
#include <bitset>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(),v.end()
using namespace std;

LL binpow(LL a,LL n)
{
	if(n==0)
		return 1;
	if(n%2==0)
        return binpow((a*a)%mod,n/2);
    return (a*binpow(a,n-1))%mod;
}

LL inv(LL x)
{
	return binpow(x,mod-2);
}

LL h[100005],pr=7919,a=1;
int p[100005][30],lst[30];
int n;
string s,ans;
vector<int> st;
map<LL,vector<int>> used;

void dfs(int l,int r)
{
	if(l>r)
		return;
	if(s[l]==s[r])
	{
        ans[l]='(';
        ans[r]=')';
		//cout<<"! "<<ans<<endl;
        dfs(l+1,r-1);
		return;
	}
    int m=p[r][s[l]-'a'];
    dfs(l,m);
    dfs(m+1,r);
}

int main()
{
    cin>>s;
    n=s.length();
    s="#"+s;
    ans=s;
    for(int i=1;i<=n;i++)
		if(!st.empty() && s[st.back()]==s[i])
		{
            h[i]=(h[i-1]+mod-((s[st.back()]-'a'+1ll)*a)%mod)%mod;
            a*=inv(pr);
            a%=mod;
            st.pop_back();
		}
		else
		{
			st.PB(i);
			a*=pr;
			a%=mod;
            h[i]=(h[i-1]+((s[i]-'a'+1ll)*a)%mod)%mod;
		}

    if(!st.empty())
	{
		cout<<-1<<endl;
        return 0;
	}
    for(int i=0;i<=n;i++)
		used[h[i]].PB(i);

    for(auto it=used.begin();it!=used.end();++it)
	{
        for(int j=0;j<26;j++)
			lst[j]=0;
        vector<int> x=it->second;
        for(int i=0;i<x.size();i++)
		{
			for(int j=0;j<26;j++)
				if(lst[j]!=-1)
					p[x[i]][j]=lst[j];
			//cout<<"!! "<<x[i]<<" "<<s[x[i]]<<endl;
            lst[s[x[i]]-'a']=x[i];
		}
	}

	dfs(1,n);
    for(int i=1;i<=n;i++)
		printf("%c",ans[i]);
	cout<<endl;
	return 0;
}


/*



bcabccddeffebgabccddeffebggagacddeffeb
cabccddeffebgabccddeffebggagacddeffe


*/

Compilation message

match.cpp: In function 'int main()':
match.cpp:98:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<x.size();i++)
                     ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 6 ms 1400 KB Output is correct
9 Correct 6 ms 1528 KB Output is correct
10 Correct 7 ms 1656 KB Output is correct
11 Correct 7 ms 1528 KB Output is correct
12 Correct 67 ms 11208 KB Output is correct
13 Correct 69 ms 12148 KB Output is correct
14 Correct 73 ms 13044 KB Output is correct
15 Correct 38 ms 11124 KB Output is correct
16 Correct 40 ms 11124 KB Output is correct
17 Correct 72 ms 13812 KB Output is correct
18 Correct 45 ms 12404 KB Output is correct
19 Correct 105 ms 17276 KB Output is correct
20 Correct 68 ms 11384 KB Output is correct
21 Correct 105 ms 17872 KB Output is correct