Submission #74018

# Submission time Handle Problem Language Result Execution time Memory
74018 2018-08-29T15:45:16 Z ikura355 Match (CEOI16_match) C++17
100 / 100
969 ms 56592 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
int n;
char s[maxn];
int sz, stk[maxn];
int rec[30][maxn];
unordered_map<int,int> pos[30];
char res[maxn];
ll add(ll x, ll y) {
	return ((x+y)%mod + mod)%mod;
}
ll mul(ll x, ll y) {
	return ((x*y)%mod + mod)%mod;
}
ll inv(ll x, ll y) {
	return 1<x ? y - inv(y%x,x)*y/x : 1;
}
void solve(int l, int r) {
	if(l>r) return ;
//	printf("solve %d %d\n",l,r);
	int x = rec[s[l]-'a'][r];
    res[l] = '('; res[x] = ')';
	if(l+1!=r) {
        solve(l+1,x-1);
        solve(x+1,r);
	}
}
int main() {
	scanf(" %s",s+1);
	n = strlen(s+1);
	ll hsh = 0;
	for(int i=1;i<=n;i++) {
		if(sz && stk[sz]==s[i]) {
			sz--;
			hsh = add(hsh, -s[i]);
			hsh = mul(hsh, inv(101,mod));
		}
		else {
            stk[++sz] = s[i];
            hsh = add(mul(hsh,101), s[i]);
		}
		pos[s[i]-'a'][hsh] = i;
		for(int x=0;x<26;x++) rec[x][i] = pos[x][hsh];
//		printf("hsh = %d\n",(int)hsh);
	}
	if(hsh!=0) return !printf("-1");
	solve(1,n);
	printf("%s",res+1);
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %s",s+1);
  ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 480 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 480 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Correct 6 ms 1100 KB Output is correct
5 Correct 5 ms 1360 KB Output is correct
6 Correct 4 ms 1360 KB Output is correct
7 Correct 6 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 480 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Correct 6 ms 1100 KB Output is correct
5 Correct 5 ms 1360 KB Output is correct
6 Correct 4 ms 1360 KB Output is correct
7 Correct 6 ms 1368 KB Output is correct
8 Correct 18 ms 3724 KB Output is correct
9 Correct 25 ms 4320 KB Output is correct
10 Correct 35 ms 5864 KB Output is correct
11 Correct 49 ms 5872 KB Output is correct
12 Correct 756 ms 41968 KB Output is correct
13 Correct 687 ms 44716 KB Output is correct
14 Correct 765 ms 46516 KB Output is correct
15 Correct 75 ms 46516 KB Output is correct
16 Correct 86 ms 46516 KB Output is correct
17 Correct 506 ms 46516 KB Output is correct
18 Correct 94 ms 46516 KB Output is correct
19 Correct 969 ms 56592 KB Output is correct
20 Correct 528 ms 56592 KB Output is correct
21 Correct 734 ms 56592 KB Output is correct