# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
51022 | 2018-06-15T15:46:41 Z | SpaimaCarpatilor | 괄호 문자열 (CEOI16_match) | C++17 | 2 ms | 440 KB |
#include<bits/stdc++.h> using namespace std; int N, t[100009], nxt[100009][26], lst[100009][26]; char sir[100009], ans[100009]; int main () { //freopen ("input", "r", stdin); //freopen ("output", "w", stdout); scanf ("%s", sir + 1), N = strlen (sir + 1); for (int i=1; i<=N; i++) sir[i] -= 'a'; for (int i=N; i>=1; i--) { t[i] = nxt[i + 1][sir[i]]; nxt[i][sir[i]] = i; if (t[i] != 0) { for (int c=0; c<26; c++) if (c != sir[i]) nxt[i][c] = nxt[t[i] + 1][c]; } } int maybe = t[1]; while (maybe < N && maybe != 0) maybe = t[maybe + 1]; if (maybe != N) { printf ("-1\n"); return 0; } for (int i=N; i>=1; i--) for (int j=0; j<26; j++) if (nxt[i][j] != 0) { int k = nxt[i + 1][j]; if (lst[k + 1][j] != 0) lst[i][j] = lst[k + 1][j]; else lst[i][j] = k; } for (int i=1; i<=N; i++) if (ans[i] == 0) { int j = lst[i][sir[i]]; ans[i] = '(', ans[j] = ')'; /*for (int k=1; k<=N; k++) if (ans[k] == 0) printf ("."); else printf ("%c", ans[k]); printf ("\n");*/ } printf ("%s\n", ans + 1); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Incorrect | 2 ms | 440 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Incorrect | 2 ms | 440 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Incorrect | 2 ms | 440 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |