Submission #127217

# Submission time Handle Problem Language Result Execution time Memory
127217 2019-07-09T06:50:32 Z roseanne_pcy Match (CEOI16_match) C++14
100 / 100
17 ms 12892 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;

const int maxn = 1e5+5;

char S[maxn];
int n;

int dp[maxn][30];

char ans[maxn];

bool valid()
{
	stack<char> s;
	for(int i = 0; i< n; i++)
	{
		if(s.empty() || s.top() != S[i])
		{
			s.push(S[i]);
		}
		else s.pop();
	}
	return s.empty();
}

void solve(int L, int R)
{
	if(L> R) return;
	int opt = dp[R][S[L]-'a'];
	// printf("[%d %d] match %d\n", L, R, opt);
	assert(opt> L);
	ans[L] = '('; ans[opt] = ')';
	solve(L+1, opt-1); solve(opt+1, R);
}

int main()
{
	scanf("%s", S);
	n = strlen(S);
	if(!valid())
	{
		printf("-1\n"); return 0;
	}
	for(int j = 0; j< 26; j++)
	{
		if(S[0] == 'a'+j) dp[0][j] = 0;
		else dp[0][j] = -1;
	}
	for(int i = 1; i< n; i++)
	{
		int x = dp[i-1][S[i]-'a'];
		// printf("x = %d\n", x);
		for(int j = 0; j< 26; j++)
		{
			if(S[i] == 'a'+j) dp[i][j] = i;
			else if(x >= 1) dp[i][j] = dp[x-1][j];
			else dp[i][j] = -1;
			// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
		}
	}
	solve(0, n-1);
	ans[n] = '\0';
	printf("%s\n", ans);
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", S);
  ~~~~~^~~~~~~~~
# 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 2 ms 552 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 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 2 ms 552 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 3 ms 1144 KB Output is correct
9 Correct 3 ms 1144 KB Output is correct
10 Correct 3 ms 1272 KB Output is correct
11 Correct 3 ms 1288 KB Output is correct
12 Correct 11 ms 7904 KB Output is correct
13 Correct 11 ms 8568 KB Output is correct
14 Correct 12 ms 9208 KB Output is correct
15 Correct 14 ms 10616 KB Output is correct
16 Correct 14 ms 10616 KB Output is correct
17 Correct 15 ms 11256 KB Output is correct
18 Correct 16 ms 11512 KB Output is correct
19 Correct 17 ms 12176 KB Output is correct
20 Correct 11 ms 8028 KB Output is correct
21 Correct 17 ms 12892 KB Output is correct