Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // clang-format off
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <list>
- #include <unordered_map>
- #include <unordered_set>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define sqrt sqrtl
- #define F first
- #define S second
- #define endl '\n'
- #define all(vc666) vc666.begin(), vc666.end()
- #define allr(vc666) vc666.rbegin(), vc666.rend()
- #define int long long
- #define degug(x) cerr (#x) << " " << (x) << endl;
- const ll INF = (ll)2e18;
- const ll inf = 1e9 + 7;
- const ll ONE = 1;
- const ll mod = 998244353;
- const ll LG = 20;
- ld EPS = 1e-12;
- ld PI = 3.1415926535897932384;
- mt19937_64 gen(rand() + rand());
- int binpow(int a, int n) {
- int res = 1;
- while (n) {
- if (n & 1) {
- res = (1ll * res * a) % mod;
- }
- a = (1ll * a * a) % mod;
- n /= 2;
- }
- return res;
- }
- const int N = 15 + 2;
- string cort[N];
- string dp[N][N][N];
- // time of play, player pos, bakance stroki -> max len psp
- pair <bool, bool> can[N][N][N];
- // time of play, player pos, bakance stroki -> are we in game and can we stop now
- void go_count(int pos, int time, int boost, int balance) {
- if (cort[time - 1][pos + boost] == '*') {
- can[time][pos][balance].second = true;
- }
- else if (cort[time - 1][pos + boost] == '(') {
- dp[time][pos][balance] += "(";
- if (dp[time - 1][pos + boost][balance + 1].size() < dp[time][pos][balance].size()) {
- dp[time - 1][pos + boost][balance + 1] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance + 1].first = true;
- }
- else if (dp[time - 1][pos + boost][balance + 1].size() == dp[time][pos][balance].size() && dp[time][pos][balance] < dp[time - 1][pos + boost][balance + 1]) {
- dp[time - 1][pos + boost][balance + 1] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance + 1].first = true;
- }
- dp[time][pos][balance].pop_back();
- }
- else if (cort[time - 1][pos + boost] == ')') {
- dp[time][pos][balance] += ")";
- if (dp[time - 1][pos + boost][balance - 1].size() < dp[time][pos][balance].size()) {
- dp[time - 1][pos + boost][balance - 1] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance - 1].first = true;
- }
- else if (dp[time - 1][pos + boost][balance - 1].size() == dp[time][pos][balance].size() && dp[time][pos][balance] < dp[time - 1][pos + boost][balance - 1]) {
- dp[time - 1][pos + boost][balance - 1] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance - 1].first = true;
- }
- dp[time][pos][balance].pop_back();
- }
- else {
- if (dp[time - 1][pos + boost][balance].size() < dp[time][pos][balance].size()) {
- dp[time - 1][pos + boost][balance] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance].first = true;
- }
- else if (dp[time - 1][pos + boost][balance].size() == dp[time][pos][balance].size() && dp[time][pos][balance] < dp[time - 1][pos + boost][balance]) {
- dp[time - 1][pos + boost][balance] = dp[time][pos][balance];
- can[time - 1][pos + boost][balance].first = true;
- }
- }
- }
- void solve() {
- int n, m, i, j, k, pos, time, balance = 0;
- cin >> n >> m;
- for (i = 0; i < n; i++) {
- cin >> cort[i];
- }
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- for (k = 0; k < N; k++) {
- dp[i][j][k] = "";
- can[i][j][k] = { false,false };
- }
- }
- }
- //cout << cort[n - 1].find('M') << endl;
- dp[n - 1][(int)cort[n - 1].find('M')][0] = {};
- can[n - 1][(int)cort[n - 1].find('M')][0] = { true, false };
- for (time = n - 1; time > 0; time--) {
- for (pos = 0; pos < m; pos++) {
- for (balance = 0; balance < n; balance++) {
- if (can[time][pos][balance].first) {
- if (pos - 1 >= 0) {
- go_count(pos, time, -1, balance);
- }
- if (pos + 1 < m) {
- go_count(pos, time, 1, balance);
- }
- go_count(pos, time, 0, balance);
- }
- }
- }
- }
- string ans = "";
- for (time = n - 1; time > 0; time--) {
- for (pos = 0; pos < m; pos++) {
- if (dp[time][pos][0].size() > ans.size() && can[time][pos][0].second) {
- ans = dp[time][pos][0];
- }
- else if (dp[time][pos][0].size() == ans.size() && can[time][pos][0].second && dp[time][pos][0] < ans) {
- ans = dp[time][pos][0];
- }
- }
- }
- for (pos = 0; pos < m; pos++) {
- if (dp[time][pos][0].size() > ans.size() && can[time][pos][0].first) {
- ans = dp[time][pos][0];
- }
- else if (dp[time][pos][0].size() == ans.size() && can[time][pos][0].second && dp[time][pos][0] < ans) {
- ans = dp[time][pos][0];
- }
- }
- cout << ans.size() << endl;
- cout << ans << endl;
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- //freopen("ladder.in", "r", stdin);
- //freopen("ladder.out", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- //cin >> t;
- while (t--) solve();
- }
- //Deisgned by skimono
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement