algorithms

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

main.cpp (1233B)


      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 
     11 // idea:
     12     // there's something about lengths of final string
     13 
     14 using namespace std;
     15 
     16 int main() {
     17     int testCases;
     18     string current = "";
     19 
     20     cin >> testCases;
     21 
     22     // cases:
     23         // the string can stay the same number of blocks
     24         // increase the number of blocks by 2
     25             // 111 -> 101 -> 101
     26         // decrease the number of blocks by 2
     27             // 101 -> 111 -> 1
     28 
     29     for(int z = 0; z < testCases; ++z) {
     30         cin >> current;
     31         int res = 1;
     32         bool dec = false;
     33         bool inc = false;
     34 
     35         for(int i = 1; i < current.size(); ++i) {
     36 
     37             if(dec && inc) {
     38                 break;
     39             }
     40 
     41             // flipping
     42             if(!dec && current[i-1] == current[i+1] && current[i-1] != current[i]) {
     43                 dec = true;
     44                 res += 1;
     45                 continue;
     46             }
     47             if(!inc && current[i-1] == current[i+1] && current[i-1] == current[i]) {
     48                 inc = true;
     49                 res += 1;
     50                 continue;
     51             }
     52         }
     53 
     54         cout << res << endl;
     55     }
     56 }