Skip to content

Instantly share code, notes, and snippets.

@ex
Created September 21, 2013 20:48
Show Gist options
  • Save ex/6654031 to your computer and use it in GitHub Desktop.
Save ex/6654031 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <map>
#include <cctype>
void test()
{
string text = "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.\n"
"Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts\n"
"ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,\n"
"hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai\n"
"gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx\n"
"rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys\n"
"twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.\n"
"Pi aeta'p heta Z'kx adzxr ai ri.\n"
"Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,\n"
"pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex\n"
"aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae\n"
"jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"\n"
"Z vcpa htya ai pts, \"jdzya aezp!\" Z riy'a htya tww aex pcddicyrzyg\n"
"ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta'p aex otpzq\n"
"zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.\n"
"Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)";
string freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
int len = text.size();
map<char, int> frequency;
for( int k = 0; k < len; k++ )
{
char c = toupper( text[k] );
if( isalpha( c ) ) { frequency[c]++; }
}
string freqText = "";
vector< pair<int, char> > sortedFreq;
for( map<char, int>::iterator it = frequency.begin(); it != frequency.end(); it++ )
{
sortedFreq.push_back( make_pair( it->second, it->first ) );
}
sort( sortedFreq.begin(), sortedFreq.end(), greater< pair<int, char> >() );
map<char, char> dic;
for( int k = 0; k < sortedFreq.size(); k++ )
{
////cout<< sortedFreq[k].first << " " << sortedFreq[k].second << endl;
freqText += sortedFreq[k].second;
dic[sortedFreq[k].second] = freqLang.at(k);
}
cout << freqLang << endl;
cout << freqText << endl;
string decrypted = "";
for( int k = 0; k < len; k++ )
{
bool upper = false;
char c = text[k];
if( c == toupper( c ) ) { upper = true; }
else { c = toupper( c ); }
if( dic.count( c ) )
{
if( upper ) { decrypted += dic[c]; }
else { decrypted += tolower( dic[c] ); }
}
else { decrypted += c; }
}
cout << decrypted << endl;
}
int main( int argc, char *argv[] )
{
test();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment