ADDED SRM/516-U/2A.cpp Index: SRM/516-U/2A.cpp ================================================================== --- SRM/516-U/2A.cpp +++ SRM/516-U/2A.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class NetworkXZeroOne { public: + string reconstruct(string message) + { + if( message[0]=='?' ) { + string msg = message; + msg[0] = 'o'; + if( check(msg) ) + return msg; + message[0] = 'x'; + } + check(message); + return message; + } + + bool check(string& msg) + { + for(int i=1; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const string& Expected, const string& 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(_, NetworkXZeroOne().reconstruct(message));} +int main(){ + +CASE(0) + string message = "x?x?"; + string _ = "xoxo"; +END +CASE(1) + string message = "?xo?"; + string _ = "oxox"; +END +CASE(2) + string message = "xo"; + string _ = "xo"; +END +CASE(3) + string message = "o??x??o"; + string _ = "oxoxoxo"; +END +CASE(4) + string message = "???????x"; + string _ = "oxoxoxox"; +END +/* +CASE(5) + string message = ; + string _ = ; +END +CASE(6) + string message = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/516-U/2B.cpp Index: SRM/516-U/2B.cpp ================================================================== --- SRM/516-U/2B.cpp +++ SRM/516-U/2B.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class NetworkXOneTimePad { public: + int crack(vector plaintexts, vector ciphertexts) + { + set possible_keys; + for(int i=0; i::iterator it=possible_keys.begin(); it!=possible_keys.end(); ++it) + { + const string& key = *it; + set target(plaintexts.begin(), plaintexts.end()); + for(int i=0; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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 plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = {"101", "010"}; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = 2; +END +CASE(1) + string plaintexts_[] = {"00", "01", "10", "11"}; + vector plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = {"00", "01", "10", "11"}; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = 4; +END +CASE(2) + string plaintexts_[] = {"01", "10"}; + vector plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = {"00"}; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = 2; +END +CASE(3) + string plaintexts_[] = {"000", "111", "010", "101", "110", "001"}; + vector plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = {"011", "100"}; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = 6; +END +/* +CASE(4) + string plaintexts_[] = ; + vector plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = ; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = ; +END +CASE(5) + string plaintexts_[] = ; + vector plaintexts(plaintexts_, plaintexts_+sizeof(plaintexts_)/sizeof(*plaintexts_)); + string ciphertexts_[] = ; + vector ciphertexts(ciphertexts_, ciphertexts_+sizeof(ciphertexts_)/sizeof(*ciphertexts_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/516-U/2C.cpp Index: SRM/516-U/2C.cpp ================================================================== --- SRM/516-U/2C.cpp +++ SRM/516-U/2C.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class NetworkXMessageRecovery { public: + string recover(vector messages) + { + map< char, set > prev; + for(int i=0; i cand; + for(map< char,set >::iterator it=prev.begin(); it!=prev.end(); ++it) + if( it->second.empty() ) + cand.insert(it->first); + char c = *cand.begin(); + result += c; + for(map< char,set >::iterator it=prev.begin(); it!=prev.end(); ++it) + it->second.erase(c); + prev.erase(c); + } + return result; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const string& Expected, const string& 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(_, NetworkXMessageRecovery().recover(messages));} +int main(){ + +CASE(0) + string messages_[] = {"acd", "bce"}; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = "abcde"; +END +CASE(1) + string messages_[] = {"ed", "dc", "cb", "ba"}; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = "edcba"; +END +CASE(2) + string messages_[] = {"Fox", "Ciel", "FoxCiel"}; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = "FoxCiel"; +END +CASE(3) + string messages_[] = {"a", "A"}; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = "Aa"; +END +/* +CASE(4) + string messages_[] = ; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = ; +END +CASE(5) + string messages_[] = ; + vector messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); + string _ = ; +END +*/ +} +// END CUT HERE