#include <vector> #include <string> #include <iostream> int main() { int size{0}; std::cin >> size; std::string mrowki; std::cin >> mrowki; std::vector<int> leftToRight(size, 0); std::vector<int> rightToLeft(size, 0); int oppositeCount{0}; int i{0}; for (auto it = mrowki.begin(); it != mrowki.end(); it++) { leftToRight[i++] = oppositeCount * 2 + (*it == 'P' ? 1 : 0); oppositeCount = (*it == 'P' ? oppositeCount + 1 : oppositeCount); } oppositeCount = 0; i = size - 1; for (auto it = mrowki.rbegin(); it != mrowki.rend(); it++) { rightToLeft[i--] = oppositeCount * 2 + (*it == 'P' ? 0 : 1); oppositeCount = (*it == 'P' ? oppositeCount : oppositeCount + 1); } for (int i = 0; i < size; i++) { std::cout << std::min(leftToRight[i], rightToLeft[i]) << (i + 1 == size ? "" : " "); } std::cout << std::endl; return 0; }
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 | #include <vector> #include <string> #include <iostream> int main() { int size{0}; std::cin >> size; std::string mrowki; std::cin >> mrowki; std::vector<int> leftToRight(size, 0); std::vector<int> rightToLeft(size, 0); int oppositeCount{0}; int i{0}; for (auto it = mrowki.begin(); it != mrowki.end(); it++) { leftToRight[i++] = oppositeCount * 2 + (*it == 'P' ? 1 : 0); oppositeCount = (*it == 'P' ? oppositeCount + 1 : oppositeCount); } oppositeCount = 0; i = size - 1; for (auto it = mrowki.rbegin(); it != mrowki.rend(); it++) { rightToLeft[i--] = oppositeCount * 2 + (*it == 'P' ? 0 : 1); oppositeCount = (*it == 'P' ? oppositeCount : oppositeCount + 1); } for (int i = 0; i < size; i++) { std::cout << std::min(leftToRight[i], rightToLeft[i]) << (i + 1 == size ? "" : " "); } std::cout << std::endl; return 0; } |