Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <set>
- #include <map>
- #include <fstream>
- using namespace std;
- int searchLeft(string s, int k) {
- do {
- --k;
- } while (k >= 0 && s[k] == 'V');
- return k+1;
- }
- int searchRight(string s, int k) {
- do {
- ++k;
- } while (k < s.size() && s[k] == 'V');
- return k-1;
- }
- int main() {
- int n;
- string s;
- cin >> n >> s;
- vector<int> steps(n, 0);
- int prev = 0;
- int step_cnt = 0, d = 0;
- do {
- d = 0;
- for (int i = 0; i < n; ++i) {
- if (s[i] != 'K')
- continue;
- int l = searchLeft(s, i),
- r = searchRight(s, i);
- if (i - l <= r - i) {
- // move left
- steps[i] = l-i + steps[prev];
- prev = i;
- } else {
- // move right
- if (s[r] == 'K')
- continue;
- steps[i] = r - l;
- prev = 0;
- }
- }
- for (int i = 0; i < n; ++i) {
- if (steps[i] != 0) {
- d = d + abs(steps[i]);
- char c = s[i];
- s[i] = s[i + steps[i]];
- s[i + steps[i]] = c;
- }
- }
- step_cnt = step_cnt + d;
- cout << s << endl;
- } while (d > 0);
- cout << s << endl;
- cout << step_cnt << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement