Submission #129022

# Submission time Handle Problem Language Result Execution time Memory
129022 2019-07-11T12:40:50 Z davitmarg Match (CEOI16_match) C++17
37 / 100
2000 ms 17780 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=4651,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 376 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 376 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 2 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 376 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 2 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 6 ms 1656 KB Output is correct
12 Correct 60 ms 11256 KB Output is correct
13 Correct 63 ms 12272 KB Output is correct
14 Correct 62 ms 13172 KB Output is correct
15 Correct 37 ms 11124 KB Output is correct
16 Correct 37 ms 11252 KB Output is correct
17 Correct 66 ms 13812 KB Output is correct
18 Correct 43 ms 12560 KB Output is correct
19 Correct 98 ms 17268 KB Output is correct
20 Correct 62 ms 11384 KB Output is correct
21 Execution timed out 2049 ms 17780 KB Time limit exceeded