#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; bool* isLeft = new bool[n+1]; cin.ignore(); char ch; for (int i=0; i<n; i++) { cin >> ch; isLeft[i] = (ch == 'L'); } unsigned int* p_on_left = new unsigned int[n]; unsigned int l_on_right; unsigned int current = 0; for (int i=0; i<n; i++) { p_on_left[i] = 2*current + (!isLeft[i] ? 1: 0); if (!isLeft[i]) { current++; } } current = 0; for (int i=n-1; i>=0; i--) { l_on_right = 2*current + (isLeft[i] ? 1: 0); if (p_on_left[i] > l_on_right) { p_on_left[i] = l_on_right; } if (isLeft[i]) { current++; } } for (int i=0; i<n; i++) { cout << p_on_left[i] << " "; } cout << "\n"; delete[] p_on_left; delete[] isLeft; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; bool* isLeft = new bool[n+1]; cin.ignore(); char ch; for (int i=0; i<n; i++) { cin >> ch; isLeft[i] = (ch == 'L'); } unsigned int* p_on_left = new unsigned int[n]; unsigned int l_on_right; unsigned int current = 0; for (int i=0; i<n; i++) { p_on_left[i] = 2*current + (!isLeft[i] ? 1: 0); if (!isLeft[i]) { current++; } } current = 0; for (int i=n-1; i>=0; i--) { l_on_right = 2*current + (isLeft[i] ? 1: 0); if (p_on_left[i] > l_on_right) { p_on_left[i] = l_on_right; } if (isLeft[i]) { current++; } } for (int i=0; i<n; i++) { cout << p_on_left[i] << " "; } cout << "\n"; delete[] p_on_left; delete[] isLeft; } |