mitkonikov

Piramida

Jan 31st, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. // MENDO Problem:
  2. // https://mendo.mk/Task.do?id=869
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int n;
  10.     cin >> n;
  11.  
  12.     map<  vector<int>, int  > dp;
  13.     vector<  pair<int, int>  > strings(n);
  14.     vector<string> stringsMain(n);
  15.     vector<int> score(n, 0);
  16.     vector<int> nextString(n, -1);
  17.     vector<int> foundStartingPointsIndex;
  18.  
  19.     for (int i = 0; i < n; ++i) {
  20.         string currentString;
  21.         cin >> currentString;
  22.  
  23.         strings[i] = make_pair(currentString.length(), i);
  24.         stringsMain[i] = currentString;
  25.         score[i] = 0;
  26.  
  27.         vector<int> currentVec(26, 0);
  28.         for (int k = 0; k < currentString.length(); k++)
  29.         {
  30.             currentVec[currentString[k] - 'A']++;
  31.         }
  32.  
  33.         dp[currentVec] = i + 1;
  34.     }
  35.  
  36.     std::map<  vector<int>, int  >::reverse_iterator it;
  37.     for (it = dp.rbegin(); it != dp.rend(); ++it) {
  38.         int stringIndex = it->second - 1;
  39.  
  40.         if (stringIndex == -1) continue;
  41.  
  42.         if (strings[stringIndex].first == 1) continue;
  43.  
  44.         vector<int> currentVec = it->first;
  45.         vector<int> mod = currentVec;
  46.         bool taken = false;
  47.  
  48.         for (int k = 0; k < 26; k++)
  49.         {            
  50.             if (taken) {
  51.                 mod[k - 1]++;
  52.             }
  53.            
  54.             if (currentVec[k] > 0) {
  55.                 mod[k]--;
  56.                 taken = true;
  57.  
  58.                 int lowerIndex = dp[mod] - 1;
  59.                 if (lowerIndex == -1) continue;
  60.  
  61.                 if (score[stringIndex] + 1 > score[lowerIndex]) {
  62.                     nextString[lowerIndex] = stringIndex;
  63.                     score[lowerIndex] = score[stringIndex] + 1;
  64.                 } else {
  65.                     score[lowerIndex] = score[lowerIndex];
  66.                 }
  67.             } else {
  68.                 taken = false;
  69.             }
  70.         }
  71.        
  72.         if (strings[stringIndex].first == 2) {
  73.             foundStartingPointsIndex.push_back(stringIndex);
  74.         }
  75.     }
  76.  
  77.     // FIND THE MAX SCORE
  78.     int maxScore = 0;
  79.     int maxScoreIndex = -1;
  80.     for (int i = 0; i < foundStartingPointsIndex.size(); ++i) {
  81.         int nextStringIndex = foundStartingPointsIndex[i];
  82.         if (score[nextStringIndex] > maxScore) {
  83.             maxScore = score[nextStringIndex];
  84.             maxScoreIndex = nextStringIndex;
  85.         }
  86.     }
  87.  
  88.     // PRINT
  89.     cout << maxScore + 2 << endl;
  90.  
  91.     int nextStringIndex = maxScoreIndex;
  92.     cout << stringsMain[nextStringIndex][0] << endl;
  93.     while(true) {
  94.         cout << stringsMain[nextStringIndex] << endl;
  95.         nextStringIndex = nextString[nextStringIndex];
  96.         if (nextStringIndex == -1) break;
  97.     }
  98.  
  99.     // system("pause");
  100.  
  101.     return 0;
  102.  }
Advertisement
Add Comment
Please, Sign In to add comment