답안 #124696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124696 2019-07-03T17:39:08 Z eriksuenderhauf 괄호 문자열 (CEOI16_match) C++11
100 / 100
455 ms 23160 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
const double eps = 1e-9;
char str[MAXN];
int a[MAXN], nx[MAXN][26], sz[MAXN][2][26];
ll pw[MAXN], pw2[MAXN];
map<ll,int> hsh[26], hsh2[26];

void solve(int l, int r) {
	if (l > r) return;
	if (str[l] == str[r]) {
		a[l] = 0;
		a[r] = 1;
		solve(l+1, r-1);
		return;
	}
	int mx = nx[r][str[l]-'a'];
	assert(l <= mx);
	a[mx] = 1;
	solve(l+1,mx-1);
	solve(mx+1,r);
}

int main() {
	pw[0] = pw2[0] = 1;
	for (int i = 1; i < MAXN; i++) {
		pw[i] = (pw[i-1] * 47ll) % MOD;
		pw2[i] = (pw2[i-1] * 37ll) % MOD2;
	}
	mem(nx, -1);
	scanf("%s", str+1);
	int n = strlen(str+1);
	if (n & 1) return !printf("-1\n");
	stack<int> st;
	st.push(1);
	a[1] = 0;
	//sz[0][1][str[0]-'a'] = 1;
	ll cur = (str[1]-'a'+1)*47ll % MOD;
	ll cur2 = (str[1]-'a'+1)*37ll % MOD2;
	for (int i = 2; i <= n; i++) {
		//int a = (int)st.size();
		hsh[str[i-1]-'a'][cur] = i;
		hsh2[str[i-1]-'a'][cur2] = i;
		if (!st.empty() && str[i] == str[st.top()]) {
			cur = (cur - pw[(int)st.size()] * (ll)(str[i]-'a'+1) % MOD + MOD) % MOD;
			cur2 = (cur2 - pw2[(int)st.size()] * (ll)(str[i]-'a'+1) % MOD2 + MOD2) % MOD2;
			st.pop();
		} else {
			st.push(i);
			cur = (cur + pw[(int)st.size()] * (ll)(str[i]-'a'+1) % MOD) % MOD;
			cur2 = (cur2 + pw2[(int)st.size()] * (ll)(str[i]-'a'+1) % MOD2) % MOD2;
		}
		//int d = ((int)st.size()-a+1) / 2;
		//cerr << a << " " << d << " " << i << "\n";
		for (int j = 0; j < 26; j++)
			if (hsh[j].count(cur) && hsh2[j].count(cur2) && hsh[j][cur] == hsh2[j][cur2])
				nx[i][j] = hsh[j][cur]-1;
		//sz[a][d][str[i-1]-'a'] = i;
	}
	if (!st.empty())
		return !printf("-1\n");
	/*cerr << "\n";
	for (int i = 1; i <= n; i++, cerr << "\n")
		for (int j = 0; j < 26; j++)
			if (nx[i][j] != -1)
				cerr << i << " " << j << " " << nx[i][j] << "\n";*/
	solve(1, n);
	for (int i = 1; i <= n; i++)
		if (a[i])
			printf(")");
		else
			printf("(");
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str+1);
  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 14 ms 12152 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 14 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 14 ms 12152 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 14 ms 12152 KB Output is correct
8 Correct 21 ms 12664 KB Output is correct
9 Correct 22 ms 12792 KB Output is correct
10 Correct 28 ms 12920 KB Output is correct
11 Correct 27 ms 12920 KB Output is correct
12 Correct 375 ms 19688 KB Output is correct
13 Correct 455 ms 20492 KB Output is correct
14 Correct 405 ms 20856 KB Output is correct
15 Correct 44 ms 12792 KB Output is correct
16 Correct 44 ms 12636 KB Output is correct
17 Correct 164 ms 17944 KB Output is correct
18 Correct 56 ms 12792 KB Output is correct
19 Correct 427 ms 23160 KB Output is correct
20 Correct 373 ms 19656 KB Output is correct
21 Correct 385 ms 23032 KB Output is correct