Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int side=6;
- const int alph=side*side;
- const int ZZZ=25;
- void db(string a)
- {
- for(int i=0;i<alph;i++)
- {
- cout<<a[i];
- if(i%side==5)
- {
- cout<<endl;
- }
- }
- }
- string shift(string a, char c)
- {
- for(int i=0;i<a.length();i++)
- {
- a[i]+=c;
- }
- return a;
- }
- string redukcja(string klucz)
- {
- bool tab[alph]={};
- string out="";
- for(int i=0;i<klucz.length();i++)
- {
- char current=klucz[i];
- if(tab[current]==0)
- {
- tab[current]=1;
- out+=current;
- }
- }
- for(int i=0;i<alph;i++)
- {
- if(tab[i]==0)
- {
- out+=i;
- }
- }
- return out;
- }
- string szyfrowanie(string msg, string key)
- {
- key=redukcja(key);
- int crdx[alph];
- int crdy[alph];
- char vals[side][side];
- for(int i=0;i<alph;i++)
- {
- int tx=i%side;
- int ty=i/side;
- crdx[i]=tx;
- crdy[i]=ty;
- vals[tx][ty]=key[i];
- }
- string out="";
- if(msg.length()%2==1)
- {
- msg+=ZZZ;
- }
- for(int i=0;i<msg.length();i+=2)
- {
- char a=msg[i];
- char b=msg[i+1];
- if(a==b)
- {
- b=ZZZ;
- }
- int x1=crdx[a];
- int x2=crdx[b];
- int y1=crdy[a];
- int y2=crdy[b];
- if(crdy[a]==crdy[b]) //wiersz
- {
- x1=(x1+1)%side;
- x2=(x2+1)%side;
- out+=vals[x1][y1];
- out+=vals[x2][y2];
- }
- else
- if(crdx[a]==crdx[b])
- {
- y1=(y1+1)%side;
- y2=(y2+1)%side;
- out+=vals[x1][y1];
- out+=vals[x2][y2];
- }
- else
- {
- out+=vals[x2][y1];
- out+=vals[x1][y2];
- }
- }
- return out;
- }
- int main()
- {
- string key,msg;
- cin>>key>>msg;
- key=shift(key,-'A');
- msg=shift(msg,-'A');
- string ooo=szyfrowanie(msg,key);
- ooo=shift(ooo,'A');
- key=shift(key,'A');
- cout<<ooo<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement