답안 #681405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681405 2023-01-13T03:17:37 Z vjudge1 괄호 문자열 (CEOI16_match) C++14
10 / 100
252 ms 3784 KB
#include <bits/stdc++.h>
using namespace std;
 
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
 
typedef long long ll;
typedef pair<int, int> ii;
 
const int N = 2000 + 5;
const int mod = 1e9 + 7;
vector<int> pos['z' + 1];
map<ii, int> f;
string s;
string t;
int n;
 
int dp(int l, int r)
{
    if(l > r) return 1;
    if((r - l + 1) & 1) return 0;
    if(f.count({l, r})) return f[{l, r}];
    int &res = f[{l, r}];
    
    res = 0;
    if(s[l] == s[r]) 
    {
        res |= dp(l + 1, r - 1);
        if(res) 
        {
            t[l] = '(', t[r] = ')';
            return res;
        }
    }
    int tmp = lower_bound(all(pos[s[l]]), r) - pos[s[l]].begin() - 1;
    while(tmp >= 0 && pos[s[l]][tmp] > l)
    {
        int i = pos[s[l]][tmp];
        res |= (dp(l + 1, i - 1) && dp(i + 1, r));
        if(res)
        {
            t[l] = '(', t[i] = ')';
            break;
        }
        tmp--;
    }
    return res;
}

void solve()
{
    cin >> s;
    int n = s.size();
    t.resize(n);
    for(int i = 0; i < n; i++)
        pos[s[i]].pb(i);
    
    if(dp(0, n - 1))
        cout << t;
    else 
        cout << -1;
}
 
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message

match.cpp: In function 'int dp(int, int)':
match.cpp:37:39: warning: array subscript has type 'char' [-Wchar-subscripts]
   37 |     int tmp = lower_bound(all(pos[s[l]]), r) - pos[s[l]].begin() - 1;
      |                                       ^
match.cpp:7:16: note: in definition of macro 'all'
    7 | #define all(a) a.begin(), a.end()
      |                ^
match.cpp:37:39: warning: array subscript has type 'char' [-Wchar-subscripts]
   37 |     int tmp = lower_bound(all(pos[s[l]]), r) - pos[s[l]].begin() - 1;
      |                                       ^
match.cpp:7:27: note: in definition of macro 'all'
    7 | #define all(a) a.begin(), a.end()
      |                           ^
match.cpp:37:56: warning: array subscript has type 'char' [-Wchar-subscripts]
   37 |     int tmp = lower_bound(all(pos[s[l]]), r) - pos[s[l]].begin() - 1;
      |                                                        ^
match.cpp:38:31: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |     while(tmp >= 0 && pos[s[l]][tmp] > l)
      |                               ^
match.cpp:40:25: warning: array subscript has type 'char' [-Wchar-subscripts]
   40 |         int i = pos[s[l]][tmp];
      |                         ^
match.cpp: In function 'void solve()':
match.cpp:58:17: warning: array subscript has type 'char' [-Wchar-subscripts]
   58 |         pos[s[i]].pb(i);
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 252 ms 3784 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 252 ms 3784 KB Output isn't correct
5 Halted 0 ms 0 KB -