Artifact Content
Not logged in

Artifact 8e15fec270ef292a3edf78e22ab4b8c389e2e50a


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class NetworkXOneTimePad { public:
	int crack(vector <string> plaintexts, vector <string> ciphertexts)
	{
		set<string> possible_keys;
		for(int i=0; i<plaintexts.size(); ++i)
			for(int j=0; j<ciphertexts.size(); ++j)
				possible_keys.insert(key(plaintexts[i], ciphertexts[j]));

		int cnt = 0;
		for(set<string>::iterator it=possible_keys.begin(); it!=possible_keys.end(); ++it)
		{
			const string& key = *it;
			set<string> target(plaintexts.begin(), plaintexts.end());
			for(int i=0; i<ciphertexts.size(); ++i) {
				string ci = cipher(ciphertexts[i], key);
				if( !target.count(ci) )
					goto next;
				target.erase(ci);
			}
			++cnt;
		next:;
		}
		return cnt;
	}

	string cipher(const string& a, const string& b)
	{
		string c;
		for(int i=0; i<a.size(); ++i)
			c += char(a[i]^(b[i]=='1'));
		return c;
	}

	string key(const string& a, const string& b)
	{
		string c;
		for(int i=0; i<a.size(); ++i)
			c += (a[i]==b[i] ? '0' : '1');
		return c;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, NetworkXOneTimePad().crack(plaintexts, ciphertexts));}
int main(){

CASE(0)
	string plaintexts_[] = {"110", "001"};
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = {"101", "010"};
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = 2; 
END
CASE(1)
	string plaintexts_[] = {"00", "01", "10", "11"};
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = {"00", "01", "10", "11"};
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = 4; 
END
CASE(2)
	string plaintexts_[] = {"01", "10"};
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = {"00"};
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = 2; 
END
CASE(3)
	string plaintexts_[] = {"000", "111", "010", "101", "110", "001"};
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = {"011", "100"};
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = 6; 
END
/*
CASE(4)
	string plaintexts_[] = ;
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = ;
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = ; 
END
CASE(5)
	string plaintexts_[] = ;
	  vector <string> plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); 
	string ciphertexts_[] = ;
	  vector <string> ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); 
	int _ = ; 
END
*/
}
// END CUT HERE