Submission #698541

# Submission time Handle Problem Language Result Execution time Memory
698541 2023-02-13T18:03:12 Z dattranxxx Match (CEOI16_match) C++11
100 / 100
12 ms 12488 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e5 + 5;
string s;
int n;

char ans[N];

int good(stack<char> st, int i, int j = n) {
  for (; i <= j; ++i) {
    if (st.empty() || st.top() != s[i]) {
      st.push(s[i]);
    } else {
      st.pop();
    }
  }
  return st.empty();
}

void task1() {
  stack<char> st;
  for (int i = 1; i <= n; ++i) {
    if (st.empty() || st.top() != s[i]) {
      st.push(s[i]);
      ans[i] = '(';
    } else {
      st.push(s[i]);
      ans[i] = '(';
      if (good(st, i + 1)) continue;
      st.pop();
      ans[i] = ')';
      st.pop();
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i];
  }
  exit(0);
  
}

int last[N][26];

void solve(int l, int r) {
  if (l > r) return;
  int m = last[r][s[l] - 'a'];
  if (!m) {
    cout << -1;
    exit(0);
  }
  ans[l] = '(';
  ans[m] = ')';
  solve(l + 1, m - 1);
  solve(m + 1, r);
}

int main() {
  cin.tie(0)->sync_with_stdio(0); cout.tie(0);
  cin >> s;
  n = s.size();
  s = ' ' + s;
  
  // if (!good(stack<char>(), 1)) {
    // cout << -1;
    // return 0;
  // }
  
  // if (n <= 2000) task1();

  // last[i][c] = max j sao cho [j + 1..i] tm va s[j] = c
  for (int i = 1; i <= n; ++i) {
    last[i][s[i] - 'a'] = i;
    
    if (last[i - 1][s[i] - 'a']) {
      int j = last[i - 1][s[i] - 'a'] - 1;
      for (int c = 0; c < 26; ++c) if (!last[i][c]) {
        last[i][c] = last[j][c];
      }
    }
  }
  solve(1, n);

  for (int i = 1; i <= n; ++i) {
    cout << ans[i];
  }
  
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 2 ms 980 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1236 KB Output is correct
12 Correct 6 ms 7636 KB Output is correct
13 Correct 8 ms 8348 KB Output is correct
14 Correct 9 ms 9176 KB Output is correct
15 Correct 11 ms 10840 KB Output is correct
16 Correct 11 ms 10804 KB Output is correct
17 Correct 10 ms 11436 KB Output is correct
18 Correct 12 ms 10704 KB Output is correct
19 Correct 12 ms 11600 KB Output is correct
20 Correct 7 ms 7892 KB Output is correct
21 Correct 12 ms 12488 KB Output is correct