#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer {cout << "NO"; exit(0);}
#define vectors vector<vector<llong> >
#define FOR(i, start, finish, k) for(llong i = start; i <= finish; i += k)
const long long MXN = 2e5 + 10;
const long long MNN = 2e3 + 520;
const long long MOD = 2012;
const long long INF = 1e18;
const long long OO = 4e9;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
string s;
string ans = "";
vector<int> pos[30];
//int dp[MNN][MNN];
bool rec(int l, int r){
//dp[l][r]++;
//if(dp[l][r] >= 1000000) cout << 1 / 0;
if(l > r) return true;
if(l == r) return false;
if(s[l] == s[r] && (r - l) % 2 == 1 && rec(l + 1, r - 1)){
ans[l] = '(';
ans[r] = ')';
return true;
}
int right = (lower_bound(pos[s[l] - 'a'].begin(), pos[s[l] - 'a'].end(), r) - pos[s[l] - 'a'].begin());
for(int i = right - 1; ; i--){
if(i == -1) return false;
int mid = pos[s[l] - 'a'][i];
if(l < mid && rec(l, mid) && rec(mid + 1, r)){
return true;
}
}
return false;
}
int main() {
ios;
cin >> s;
for(int i = 0; i < s.size(); i++) {
ans += '-';
pos[s[i] - 'a'].pb(i);
}
if(rec(0, s.size() - 1) == 0) cout << -1;
else cout << ans;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:76:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Execution timed out |
2015 ms |
376 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Execution timed out |
2015 ms |
376 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |