Skip to content

Instantly share code, notes, and snippets.

@anisaraf
Created September 21, 2010 04:18
Show Gist options
  • Save anisaraf/589181 to your computer and use it in GitHub Desktop.
Save anisaraf/589181 to your computer and use it in GitHub Desktop.
Srm144-300Dirty
#include<vector>
#include<iostream>
using namespace std;
class BinaryCode {
public:
vector<string> decode(string message){
vector<string> rVector;
string rString;
for(int j = 0; j < 2; j++){
rString = "";
rString.push_back(j + '0');
for(int i = 1 ; i < message.length(); i++){
char cZero = decodeChar(message, rString,i);
if(cZero != '0' && cZero != '1')
{ rString = "NONE";
break;
}
else
rString.push_back(cZero);
}
int rStringLen = rString.length();
if(rStringLen > 1 && rString!="NONE"){
if( !(message[message.length()-1] == (rString[rStringLen-1] + rString[rStringLen-2] - '0'))){
cout<<endl<<rString<<"OP";
rString = "NONE";
}
}
if(rStringLen == 1)
{
if(message[0] > 1)
rString = "NONE";
else
{
if(message[0] == '0' && j ==1)
rString="NONE";
if(message[0] == '1' && j ==0)
rString="NONE";
}
}
rVector.push_back(rString);
}
return rVector;
}
char decodeChar(string message, string decoded, int pos){
if(pos == 1) return ((message[pos-1] - '0') - (decoded[pos-1] - '0') + '0');
return ((message[pos-1] - '0') - (decoded[pos-2] - '0') - (decoded[pos-1] - '0') + '0');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment