Skip to content

Instantly share code, notes, and snippets.

@lullasy
Created September 7, 2012 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lullasy/3665986 to your computer and use it in GitHub Desktop.
Save lullasy/3665986 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class CuttingBitString {
public:
int getmin(string S) {
string data[] = {"1", "101", "11001", "1111101", "1001110001"};
cout << S << " " << S.length() << endl;;
for (int i = 0; i < 5; i++) if (data[i] == S) return 1;
int ret = 0, cnt = 0;
/*
for (int place = 0; place < S.length(); ){
bool flag = false;
for (int i = 4; 0 <= i; i--){
if (data[i] == S.substr(place, data[i].length())){
ret++;
cout << data[i] << endl;
place += data[i].length();
cnt += data[i].length();
flag = true;
break;
}
}
if (cnt == S.length()) break;
if (!flag) return -1;
}
*/
string str = S;
ret = 0, cnt = 0;
for (int i = 4; 0 <= i; i--){
for (int j = 0; j < str.length(); j++){
if (data[i] == str.substr(j, data[i].length())){
ret++;
for (int k = j; k < j+data[i].length(); k++) str[k] = '*';
cout << str << endl;
cout << data[i] << endl;
cnt += data[i].length();
}
}
}
if (cnt != str.length()) ret = 0;
return (ret == 0) ? -1 : ret;
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arg0 = "101101101"; int Arg1 = 3; verify_case(0, Arg1, getmin(Arg0)); }
void test_case_1() { string Arg0 = "1111101"; int Arg1 = 1; verify_case(1, Arg1, getmin(Arg0)); }
void test_case_2() { string Arg0 = "00000"; int Arg1 = -1; verify_case(2, Arg1, getmin(Arg0)); }
void test_case_3() { string Arg0 = "110011011"; int Arg1 = 3; verify_case(3, Arg1, getmin(Arg0)); }
void test_case_4() { string Arg0 = "1000101011"; int Arg1 = -1; verify_case(4, Arg1, getmin(Arg0)); }
void test_case_5() { string Arg0 = "111011100110101100101110111"; int Arg1 = 5; verify_case(5, Arg1, getmin(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main(){
CuttingBitString ___test;
___test.run_test(-1);
}
//END CUT HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment