Advertisement
Manh_LK

Digo plays with Numbers

Apr 22nd, 2020
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int t, n, k;
  5. string s;
  6. bool check()
  7. {
  8.     bool check0 = false;
  9.     bool check1 = false;
  10.     for (int i = 0; i < n; i++)
  11.     {
  12.         if (s[i] == '0') check0 = true;
  13.         if (s[i] == '1') check1 = true;
  14.         if (check0 &&check1) return true;
  15.     }
  16.     return false;
  17. }
  18. void slove(char bit)
  19. {
  20.     if (!check())
  21.     {
  22.         int index = 0;
  23.         while (s[index] == 'a')
  24.         {
  25.             index++;
  26.         }
  27.         s[index] = 'a';
  28.     }
  29.     else
  30.     {
  31.         for (int i = 0; i < n; i++)
  32.         {
  33.             if (s[i] == bit) { s[i] = 'a'; break; }
  34.         }
  35.     }
  36. }
  37. void print()
  38. {
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         if (s[i] != 'a') cout << s[i];
  42.     }
  43.     cout << endl;
  44. }
  45. int main()
  46. {
  47.     freopen("input.txt", "r", stdin);
  48.     cin >> t;
  49.     for (int tc = 0; tc < t; tc++)
  50.     {
  51.         cin >> n >> k >> s;
  52.         int N = n, cnt = 0;
  53.         while (N != k)
  54.         {
  55.             if (cnt % 2 == 0) slove('1');
  56.             else slove('0');
  57.             cnt++; N--;
  58.         }
  59.         print();
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement