algorithms

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

main.cpp (1550B)


      1 #include <iostream>
      2 
      3 // hold up. what answers do we **ever** get?
      4 // 
      5 // -1, 1, 2, 3
      6 // 
      7 // if we were going to get 4, we made a mistake
      8 // 
      9 // we know -1 can be handled easily
     10 // 
     11 // 1: this is the case when we don't have +- or -+.
     12 // 
     13 // 2: this is the case when we have +- or -+, but not 3
     14 // 
     15 // 3: 
     16 // 
     17 // +--+
     18 // 
     19 // -++-
     20 
     21 int solve(std::string& input) {
     22 
     23     if(input[0] == '0') {
     24         return -1;
     25     }
     26 
     27     for(int i = 0; i < (int)input.size()-1; ++i) {
     28         if(input[i] == '0' && input[i+1] == '0') {
     29             return -1;
     30         }
     31     }
     32 
     33     for(int i = 0; i < (int)input.size() - 3; ++i) {
     34         if(input[i] == '+' && input[i+1] == '-' && input[i+2] == '-' && input[i+3] == '+') {
     35             return 3;
     36         }
     37         if(input[i] == '-' && input[i+1] == '+' && input[i+2] == '+' && input[i+3] == '-') {
     38             return 3;
     39         }
     40     }
     41 
     42     for(int i = 0; i < (int)input.size(); i += 2) {
     43         if(input[i] == '0') {
     44             return 2;
     45         }
     46     }
     47 
     48     for(int i = 0; i < (int)input.size()-1; ++i) {
     49         if((input[i] == '+' && input[i+1] == '-') || (input[i] == '-' && input[i+1] == '+')) {
     50             return 2;
     51         }
     52     }
     53 
     54     return 1;
     55 }
     56 
     57 
     58 int main() {
     59     int testCases;
     60     int lenS;
     61     std::cin >> testCases;
     62 
     63     for(int z = 0 ; z < testCases; ++z) {
     64         std::cin >> lenS;
     65         std::string current = "";
     66         for(int i = 0 ; i < lenS; ++i) {
     67             char c;
     68             std::cin >> c;
     69             current += c;
     70         }
     71         std::cout << solve(current) << std::endl;
     72     }
     73 
     74 }
     75