Advertisement
NHumme

A

Apr 8th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "negcycle"
  14.  
  15. string s;
  16. char minb[200000];
  17. int nom[200000], mind, maxk, n, idx;
  18.  
  19. int main() {
  20.  
  21. #ifdef _DEBUG
  22.     freopen("debug.in", "r", stdin);
  23.     freopen("debug.out", "w", stdout);
  24. #else
  25.     freopen(TASK".in", "r", stdin);
  26.     freopen(TASK".out", "w", stdout);
  27.     //freopen("input.txt", "r", stdin);
  28.     //freopen("output.txt", "w", stdout);
  29. #endif // _DEBUG
  30.  
  31.     ios_base::sync_with_stdio(0);
  32.     cin.tie(0);
  33.     cout.tie(0);
  34.     cout.precision(6);
  35.  
  36.    
  37.     cin >> s;
  38.     n = s.size();
  39.     minb[0] = s[0];
  40.     nom[0] = 0;
  41.     for (int i = 1; i<n; i++)
  42.     {
  43.         if (s[i] <= minb[i - 1])
  44.         {
  45.             minb[i] = s[i];
  46.             nom[i] = i;
  47.         }
  48.         else
  49.         {
  50.             minb[i] = minb[i - 1];
  51.             nom[i] = nom[i - 1];
  52.         }
  53.     }
  54.     maxk = s[n - 1] - minb[n - 1];
  55.     idx = nom[n - 1];
  56.     mind = n - nom[n - 1];
  57.     for (int i = n - 2; i >= 0; i--)
  58.     {
  59.         if (s[i] - minb[i]==maxk && mind > i - nom[i] || s[i] - minb[i] > maxk)
  60.         {
  61.             idx = nom[i];
  62.             mind = i - nom[i] + 1;
  63.             maxk = s[i] - minb[i];
  64.         }
  65.     }
  66.     cout << s.substr(idx, mind);
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement