algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

main.cpp (1960B)


      1 #include <iostream>
      2 #include <vector>
      3 #include <unordered_set>
      4 #include <unordered_map>
      5 #include <algorithm>
      6 #include <queue>
      7 
      8 #define ll long long
      9 
     10 using namespace std;
     11 
     12 void printLs(vector<char>& ls) {
     13     for(int i = 0; i  < ls.size(); ++i) {
     14         cout << ls[i];
     15     }
     16     cout << endl;
     17 }
     18 
     19 
     20 
     21 int main() {
     22 
     23     int testCases, n,k;
     24     cin >> testCases;
     25 
     26     for(int z = 0; z < testCases; ++z) {
     27         cin >> n;
     28         cin >> k;
     29         vector<char> ls {};
     30         for(int i = 0; i < n; ++i) {
     31             ls.push_back('+');
     32         }
     33 
     34         string instructions = "";
     35         cin >> instructions;
     36 
     37         // this is a second attempt, following the editorial, prior worked correctly, just complicated
     38         int countTopRemove = 0;
     39         int countBottomRemove = 0;
     40         int countUnknown = 0;
     41         for(int i = 0; i < instructions.size(); ++i) {
     42             // i got top and bottom  mixed up in my removal logic so I swapped these, but it all works like this.
     43             if(instructions[i] == '0') {
     44                  countBottomRemove += 1;
     45             }
     46             else if(instructions[i] == '1') {
     47                  countTopRemove += 1;
     48             } else {
     49                 countUnknown += 1;
     50             }
     51         }
     52         for(int i = 0; i < countBottomRemove && i < ls.size(); ++i) {
     53             ls[i] = '-';
     54         }
     55         for(int i = ls.size() - 1; i > ((ls.size() - 1) - countTopRemove) && i >= 0; --i) {
     56             ls[i] = '-';
     57         }
     58         for(int i = 0; i < countUnknown; ++i) {
     59             ls[i+countBottomRemove] =  '?';
     60             int idx = ((ls.size()-1) - countTopRemove) - i;
     61             if(idx >= 0 && idx < ls.size()) {
     62                 ls[((ls.size()-1) - countTopRemove) - i] =  '?';
     63             }
     64         }
     65         if(countUnknown+countTopRemove+countBottomRemove >= ls.size()) {
     66             for(int i = 0; i < ls.size(); ++i) {
     67                 ls[i] = '-';
     68             }
     69         }
     70 
     71         printLs(ls);
     72     }
     73 }