Advertisement
skimono

Untitled

Jan 11th, 2023
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1.  
  2. // clang-format off
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <stack>
  11. #include <iomanip>
  12. #include <fstream>
  13. #include <string>
  14. #include <set>
  15. #include <deque>
  16. #include <queue>
  17. #include <map>
  18. #include <bitset>
  19. #include <random>
  20. #include <list>
  21. #include <unordered_map>
  22. #include <unordered_set>
  23. #include <cassert>
  24.  
  25. using namespace std;
  26.  
  27. typedef long long ll;
  28. typedef unsigned long long ull;
  29. typedef long double ld;
  30. typedef string str;
  31. //typedef __int128 ultraint;
  32. #define sqrt sqrtl
  33. #define F first
  34. #define S second
  35. #define endl '\n'
  36. #define all(vc666) vc666.begin(), vc666.end()
  37. #define allr(vc666) vc666.rbegin(), vc666.rend()
  38. #define int long long
  39. #define degug(x) cerr (#x) << " " << (x) << endl;
  40.  
  41. const ll INF = (ll)2e18;
  42. const ll inf = 1e9 + 7;
  43. const ll ONE = 1;
  44. const ll mod = 998244353;
  45. const ll LG = 20;
  46. ld EPS = 1e-12;
  47. ld PI = 3.1415926535897932384;
  48. mt19937_64 gen(rand() + rand());
  49.  
  50. int binpow(int a, int n) {
  51.     int res = 1;
  52.     while (n) {
  53.         if (n & 1) {
  54.             res = (1ll * res * a) % mod;
  55.         }
  56.         a = (1ll * a * a) % mod;
  57.         n /= 2;
  58.     }
  59.     return res;
  60. }
  61.  
  62. const int N = 15 + 2;
  63. string cort[N];
  64. string dp[N][N][N];
  65. // time of play, player pos, bakance stroki -> max len psp
  66. pair <bool, bool> can[N][N][N];
  67. // time of play, player pos, bakance stroki -> are  we in game and can we stop now
  68. void go_count(int pos, int time, int boost, int balance) {
  69.     if (cort[time - 1][pos + boost] == '*') {
  70.         can[time][pos][balance].second = true;
  71.     }
  72.     else if (cort[time - 1][pos + boost] == '(') {
  73.         dp[time][pos][balance] += "(";
  74.         if (dp[time - 1][pos + boost][balance + 1].size() < dp[time][pos][balance].size()) {
  75.             dp[time - 1][pos + boost][balance + 1] = dp[time][pos][balance];
  76.             can[time - 1][pos + boost][balance + 1].first = true;
  77.         }
  78.         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]) {
  79.             dp[time - 1][pos + boost][balance + 1] = dp[time][pos][balance];
  80.             can[time - 1][pos + boost][balance + 1].first = true;
  81.         }
  82.         dp[time][pos][balance].pop_back();
  83.     }
  84.     else if (cort[time - 1][pos + boost] == ')') {
  85.         dp[time][pos][balance] += ")";
  86.         if (dp[time - 1][pos + boost][balance - 1].size() < dp[time][pos][balance].size()) {
  87.             dp[time - 1][pos + boost][balance - 1] = dp[time][pos][balance];
  88.             can[time - 1][pos + boost][balance - 1].first = true;
  89.         }
  90.         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]) {
  91.             dp[time - 1][pos + boost][balance - 1] = dp[time][pos][balance];
  92.             can[time - 1][pos + boost][balance - 1].first = true;
  93.         }
  94.         dp[time][pos][balance].pop_back();
  95.     }
  96.     else {
  97.         if (dp[time - 1][pos + boost][balance].size() < dp[time][pos][balance].size()) {
  98.             dp[time - 1][pos + boost][balance] = dp[time][pos][balance];
  99.             can[time - 1][pos + boost][balance].first = true;
  100.         }
  101.         else if (dp[time - 1][pos + boost][balance].size() == dp[time][pos][balance].size() && dp[time][pos][balance] < dp[time - 1][pos + boost][balance]) {
  102.             dp[time - 1][pos + boost][balance] = dp[time][pos][balance];
  103.             can[time - 1][pos + boost][balance].first = true;
  104.         }
  105.     }
  106. }
  107.  
  108. void solve() {
  109.     int n, m, i, j, k, pos, time, balance = 0;
  110.     cin >> n >> m;
  111.     for (i = 0; i < n; i++) {
  112.         cin >> cort[i];
  113.     }
  114.     for (i = 0; i < N; i++) {
  115.         for (j = 0; j < N; j++) {
  116.             for (k = 0; k < N; k++) {
  117.                 dp[i][j][k] = "";
  118.                 can[i][j][k] = { false,false };
  119.             }
  120.         }
  121.     }
  122.     //cout << cort[n - 1].find('M') << endl;
  123.     dp[n - 1][(int)cort[n - 1].find('M')][0] = {};
  124.     can[n - 1][(int)cort[n - 1].find('M')][0] = { true, false };
  125.     for (time = n - 1; time > 0; time--) {
  126.         for (pos = 0; pos < m; pos++) {
  127.             for (balance = 0; balance < n; balance++) {
  128.                 if (can[time][pos][balance].first) {
  129.                     if (pos - 1 >= 0) {
  130.                         go_count(pos, time, -1, balance);
  131.                     }
  132.                     if (pos + 1 < m) {
  133.                         go_count(pos, time, 1, balance);
  134.                     }
  135.                     go_count(pos, time, 0, balance);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.     string ans = "";
  141.     for (time = n - 1; time > 0; time--) {
  142.         for (pos = 0; pos < m; pos++) {
  143.             if (dp[time][pos][0].size() > ans.size() && can[time][pos][0].second) {
  144.                 ans = dp[time][pos][0];
  145.             }
  146.             else if (dp[time][pos][0].size() == ans.size() && can[time][pos][0].second && dp[time][pos][0] < ans) {
  147.                 ans = dp[time][pos][0];
  148.             }
  149.         }
  150.     }
  151.     for (pos = 0; pos < m; pos++) {
  152.         if (dp[time][pos][0].size() > ans.size() && can[time][pos][0].first) {
  153.             ans = dp[time][pos][0];
  154.         }
  155.         else if (dp[time][pos][0].size() == ans.size() && can[time][pos][0].second && dp[time][pos][0] < ans) {
  156.             ans = dp[time][pos][0];
  157.         }
  158.     }
  159.     cout << ans.size() << endl;
  160.     cout << ans << endl;
  161. }
  162.  
  163. signed main() {
  164. #ifdef _DEBUG
  165.     freopen("input.txt", "r", stdin);
  166.     freopen("output.txt", "w", stdout);
  167. #endif
  168.     //freopen("ladder.in", "r", stdin);
  169.     //freopen("ladder.out", "w", stdout);
  170.     ios_base::sync_with_stdio(0);
  171.     cin.tie(NULL);
  172.     cout.tie(NULL);
  173.     int t = 1;
  174.     //cin >> t;
  175.     while (t--) solve();
  176. }
  177. //Deisgned by skimono
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement