Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <set>
- #include <stack>
- #include <queue>
- #include <deque>
- using namespace std;
- #define TASK "negcycle"
- string s;
- char minb_p[200000], minb_s[200000];
- int nom_p[200000], nom_s[200000], mind, maxk, n, idx;
- int main() {
- #ifdef _DEBUG
- freopen("debug.in", "r", stdin);
- freopen("debug.out", "w", stdout);
- #else
- //freopen(TASK".in", "r", stdin);
- //freopen(TASK".out", "w", stdout);
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif // _DEBUG
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout.precision(6);
- cin >> s;
- n = s.size();
- minb_p[0] = s[0];
- nom_p[0] = 0;
- minb_s[n - 1] = s[n - 1];
- nom_s[n - 1] = n - 1;
- for (int i = 1; i<n; i++)
- {
- if (s[i] <= minb_p[i - 1])
- {
- minb_p[i] = s[i];
- nom_p[i] = i;
- }
- else
- {
- minb_p[i] = minb_p[i - 1];
- nom_p[i] = nom_p[i - 1];
- }
- }
- for (int i = n - 2; i >= 0; i--)
- {
- if (s[i] <= minb_s[i + 1])
- {
- minb_s[i] = s[i];
- nom_s[i] = i;
- }
- else
- {
- minb_s[i] = minb_s[i + 1];
- nom_s[i] = nom_s[i + 1];
- }
- }
- maxk = 0;
- mind = 1000000;
- for (int i = n - 1; i >= 0; i--)
- {
- if (s[i] - minb_p[i]==maxk && mind > i - nom_p[i] + 1 || s[i] - minb_p[i] > maxk)
- {
- idx = nom_p[i];
- mind = i - nom_p[i] + 1;
- maxk = s[i] - minb_p[i];
- }
- if (s[i] - minb_s[i] == maxk && mind > nom_s[i] - i + 1 || s[i] - minb_s[i] > maxk)
- {
- idx = i;
- mind = nom_s[i] - i + 1;
- maxk = s[i] - minb_s[i];
- }
- }
- cout << s.substr(idx, mind);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement