ã©ã³ã¬ã³ã°ã¹å§ç¸®ã§è§£ãã
åé¡æ¦è¦
0 㨠1 ãããªãé·ã ã®æåå ãä¸ããããããã®æååã®ä¸ã§ã®ã1 ã®å¡ãã®ãã¡ãå·¦ãã çªç®ã®ãã®ãã çªç®ã®ãã®ã®å³ã«ç§»åãããã
ä¾ï¼"010011100011001" â "010011111000001"
å¶ç´
èãããã¨
ã¾ããæåå ãã©ã³ã¬ã³ã°ã¹å§ç¸®ããããããããã¨ã
"010011100011001"
â (0, 1), (1, 1), (0, 2), (1, 3), (0, 3), (1, 2), (0, 2), (1, 0)
ã¨ããããã«ãªããã©ã³ã¬ã³ã°ã¹å§ç¸®ã®ããæ¹ã«ã¤ãã¦ã¯ã次ã®åé¡ã®å ¬å¼è§£èª¬ãè¦ãã¨ããã
ãã¦ãã©ã³ã¬ã³ã°ã¹å§ç¸®ã§ããã°ãã©ã³ã¬ã³ã°ã¹å§ç¸®ããåä¸ã§ã çªç®ã®ã1 ã®å¡ãã¨ããã®å·¦ã®ã0 ã®å¡ãã swap ããã°ãããä¸ã®ä¾ã§ã¯ã
(0, 1), (1, 1), (0, 2), (1, 3), (0, 3), (1, 2), (0, 2), (1, 0)
â (0, 1), (1, 1), (0, 2), (1, 3), (1, 2), (0, 3), (0, 2), (1, 0)
ã¨ããããã«ãªãããããã¦å¾ãããåãåã³ 0-1 æååã«ç´ãã° OKã
è¨ç®é㯠ã¨ãªãã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; using pint = pair<int,int>; int main() { int N, K; string S; cin >> N >> K >> S; vector<pint> lens; for (int i = 0; i < N; ) { int j = i; while (j < N && S[j] == S[i]) j++; lens.emplace_back((int)(S[i]-'0'), j - i); i = j; } int num = 0; for (int i = 0; i < lens.size(); i++) { if (lens[i].first == 1) { num++; if (num == K - 1) { swap(lens[i+1], lens[i+2]); } } } for (auto [val, num] : lens) { for (int i = 0; i < num; i++) cout << val; } cout << endl; }