#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <char> ans, dp[250][250][27][27];
char mk[250][250][27][27];
bool low(vector <char> &a, vector <char> &b)
{
for (register int i = 0; i < sz(a); i++)
{
if (a[i] == b[i]) continue;
return a[i] == '(';
}
return 0;
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
int sx, sy;
for (int i = 0; i < m; i++)
if (s[n - 1][i] == 'M') {sx = n - 1; sy = i; break;}
mk[n - 1][sy][0][0] = 'z';
for (register int i = n - 1; i >= 0; i--)
for (register int j = 0; j < m; j++)
for (register int k = 0; k < min(27, n - i); k++)
for (register int kr = 0; kr <= k; kr++)
{
if (mk[i][j][k][kr] != 'z') continue;
if (k == kr && (k + kr > sz(ans) || (k + k == sz(ans) && low(dp[i][j][k][kr], ans)))) ans = dp[i][j][k][kr];
if (s[i][j] == '*' || i == 0) continue;
for (register int a = -1; a <= 1; a++)
if (a + j >= 0 && a + j < m && (k != kr || s[i - 1][j + a] != ')'))
{
int nw = kr, nk = k;
vector <char> nr = dp[i][j][k][kr];
if (s[i - 1][j + a] == '(') {nr.pb('('); nk++;}
if (s[i - 1][j + a] == ')') {nr.pb(')'); nw++;}
if (mk[i - 1][j + a][nk][nw] != 'z' || low(nr, dp[i - 1][j + a][nk][nw])) {mk[i - 1][j + a][nk][nw] = 'z'; dp[i - 1][j + a][nk][nw] = nr;}
}
}
cout << sz(ans) << endl;
for (auto it : ans) cout << it;
}
Compilation message
retro.cpp: In function 'int main()':
retro.cpp:61:9: warning: variable 'sx' set but not used [-Wunused-but-set-variable]
int sx, sy;
^~
retro.cpp:66:25: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
mk[n - 1][sy][0][0] = 'z';
~~~~~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
244 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Runtime error |
252 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Runtime error |
253 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Runtime error |
247 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
5 |
Runtime error |
245 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Runtime error |
256 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
7 |
Runtime error |
255 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Runtime error |
247 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Runtime error |
245 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
10 |
Runtime error |
251 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Runtime error |
243 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
12 |
Runtime error |
251 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
13 |
Runtime error |
251 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Runtime error |
243 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
15 |
Runtime error |
253 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
16 |
Runtime error |
245 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
17 |
Runtime error |
245 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
18 |
Runtime error |
236 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
19 |
Runtime error |
242 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
20 |
Runtime error |
251 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |