Topcoder SRM144 Div1 Level 1 BinaryCode

解法


最初の数字から順番に決めていく
答えに0と1以外が入ってたら"NONE"
問題ないならそれが答えになる

コード


#include <bits/stdc++.h>
#include <stdint.h>
#include <sys/time.h>

class BinaryCode {
public:
  std::vector<std::string> decode(std::string message) {
    std::vector<std::string> res;
    res.push_back(solve(message, 0));
    res.push_back(solve(message, 1));
    return res;
  }
  std::string solve(std::string message, int first) {
    int ans[64] = {};
    int n = message.size();
    std::string res;

    ans[1] = first;
    for(int i = 0; i < n; ++i) {
      ans[i+2] = message[i] - '0' - ans[i] - ans[i+1];
    }

    for(int i = 1; i <= n; ++i) {
      if( not ( ans[i] == 0 or ans[i] == 1 ) ) {
        return "NONE";
      }
    }
    if( ans[n + 1] != 0 ) {
      return "NONE";
    }

    for(int i = 1; i <= n; ++i) {
      res += (char)ans[i] + '0';
    }
    return res;
  }
};