답안 #52894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52894 2018-06-27T06:45:58 Z 강태규(#1381) 괄호 문자열 (CEOI16_match) C++11
0 / 100
2 ms 248 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n;
char str[100001];
char st[100000];
char ans[100001];
int pr[100001][26];

void solve(int s, int e) {
    if (s > e) return;
    if (str[s] == str[e]) {
        ans[s] = '(';
        ans[e] = ')';
        solve(s + 1, e - 1);
    }
    else {
        int m = e - pr[e][str[e]];
        if (m <= s || str[m] != str[e]) throw 0;
        solve(s, m - 1);
        solve(m, e);
    }
}

int main() {
    scanf("%s", str);
    int tp = 0;
    for (int i = 0; str[i]; n = ++i) {
        str[i] -= 'a';
        if (tp && st[tp - 1] == str[i]) --tp;
        else st[tp++] = str[i];
    }
    if (tp) {
        printf("-1\n");
        return 0;
    }
    for (int i = 0; i < 26; ++i) {
        pr[0][i] = 1;
    }
    for (int i = 1; i < n; ++i) {
        if (str[i - 1] == str[i]) pr[i][str[i]] = 1;
        else pr[i][str[i]] = pr[i - 1][str[i]] + 1;
        for (int j = 0; j < 26; ++j) {
            if (str[i] == j) continue;
            if (i <= pr[i][str[i]]) pr[i][j] = i + 1;
            else if (str[i - pr[i][str[i]] - 1] == j)
                pr[i][j] = pr[i][str[i]] + 1;
            else
                pr[i][j] = pr[i - pr[i][str[i]] - 1][j] + pr[i][str[i]] + 1;
            printf("pr[%d][%d]=%d\n", i, j, pr[i][j]);
        }
    }
    solve(0, n - 1);
    printf(ans);
	return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:35:33: warning: array subscript has type 'char' [-Wchar-subscripts]
         int m = e - pr[e][str[e]];
                                 ^
match.cpp: In function 'int main()':
match.cpp:58:47: warning: array subscript has type 'char' [-Wchar-subscripts]
         if (str[i - 1] == str[i]) pr[i][str[i]] = 1;
                                               ^
match.cpp:59:26: warning: array subscript has type 'char' [-Wchar-subscripts]
         else pr[i][str[i]] = pr[i - 1][str[i]] + 1;
                          ^
match.cpp:59:46: warning: array subscript has type 'char' [-Wchar-subscripts]
         else pr[i][str[i]] = pr[i - 1][str[i]] + 1;
                                              ^
match.cpp:62:34: warning: array subscript has type 'char' [-Wchar-subscripts]
             if (i <= pr[i][str[i]]) pr[i][j] = i + 1;
                                  ^
match.cpp:63:42: warning: array subscript has type 'char' [-Wchar-subscripts]
             else if (str[i - pr[i][str[i]] - 1] == j)
                                          ^
match.cpp:64:40: warning: array subscript has type 'char' [-Wchar-subscripts]
                 pr[i][j] = pr[i][str[i]] + 1;
                                        ^
match.cpp:66:47: warning: array subscript has type 'char' [-Wchar-subscripts]
                 pr[i][j] = pr[i - pr[i][str[i]] - 1][j] + pr[i][str[i]] + 1;
                                               ^
match.cpp:66:71: warning: array subscript has type 'char' [-Wchar-subscripts]
                 pr[i][j] = pr[i - pr[i][str[i]] - 1][j] + pr[i][str[i]] + 1;
                                                                       ^
match.cpp:71:15: warning: format not a string literal and no format arguments [-Wformat-security]
     printf(ans);
               ^
match.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", str);
     ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 248 KB Output isn't correct
2 Halted 0 ms 0 KB -