double_trouble

skabki

Apr 18th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. vector<pair<pair<int, int>, pair<int, int> > > v;
  10. vector<vector<int> > dp;
  11.  
  12. pair<pair<int, int>, pair<int, int> > count(int i, string a)
  13. {
  14.     pair<pair<int, int>, pair<int, int> > b = { {i, a.size()}, { 0, 0 } };
  15.     for (int i = 0; i < a.size(); i++)
  16.     {
  17.         if (a[i] == '(')
  18.             b.second.second++;
  19.         else
  20.             b.second.second--;
  21.         b.second.first = min(b.second.first, b.second.second);
  22.     }
  23.     return b;
  24. }
  25.  
  26. bool cmp(const pair<pair<int, int>, pair<int, int> >& a, const pair<pair<int, int>, pair<int, int> >& b)
  27. {
  28.     return a.second.first == 0 || min(a.second.second, a.second.first + b.second.second) < min(b.second.second, b.second.first + a.second.second);
  29. }
  30.  
  31. int main()
  32. {
  33.     int n;
  34.     cin >> n;
  35.  
  36.     string c;
  37.     for (int i = 0; i < n; i++)
  38.     {
  39.         cin >> c;
  40.         pair<int, int> d = count(c);
  41.         strings f;
  42.         f.s = c;
  43.         f.bal = d.second;
  44.         f.minbal = d.first;
  45.         v.push_back(f);
  46.     }
  47.  
  48.     sort(v.begin(), v.end(), cmp);
  49.  
  50.     dp.resize(n);
  51.     for (int i = 0; i < n; i++)
  52.     {
  53.         dp[i].assign(5010, 0);
  54.     }
  55.  
  56.     dp[0][v[0].bal] = v[0].s.size();
  57.  
  58.     for (int i = 1; i < n; i++)
  59.     {
  60.         for (int j = 0; j < 5010; j++)
  61.         {
  62.             dp[i][v[i].bal + j] = max(dp[i][v[i].bal + j], dp[i - 1][j] + (int)v[i].s.size());
  63.         }
  64.     }
  65.  
  66.     cout << dp[n - 1][0];
  67.  
  68.     return 0;
  69. }
Add Comment
Please, Sign In to add comment