Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("adfgvx.in");
- ofstream fout("adfgvx.out");
- int c,messageLen,keyLen;
- char square[6][6];
- char Index[]={'A','D','F','G','V','X', '\0'};
- char key[27];
- char message[1000001];
- int main()
- {
- int i, j, k;
- fin>>c;
- fin.get();
- if(c==1)
- {
- for(i=0;i<6;++i)
- for(j=0;j<6;++j)
- fin>>square[i][j];
- fin>>message;
- fin>>key;
- messageLen=strlen(message);
- keyLen=strlen(key);
- char cipher1[2*messageLen];
- for(i=0;i<messageLen;++i)
- for(j=0;j<6;++j)
- for(k=0;k<6;++k)
- if(message[i]==square[j][k])
- cipher1[2*i]=Index[j],cipher1[2*i+1]=Index[k];
- cipher1[2*messageLen]='\0';
- int cipherLen=sizeof(cipher1);
- int rows;
- if(cipherLen%keyLen>0)
- rows=cipherLen/keyLen+1;
- else
- rows=cipherLen/keyLen;
- char A[rows][keyLen];
- i=0;j=0;
- for(k=0;k<cipherLen;++k)
- {
- if(k && k%keyLen==0)
- ++i,j=0;
- A[i][j++]=cipher1[k];
- }
- if(cipherLen%keyLen>0)
- for(j=cipherLen%keyLen; j<keyLen; ++j)
- A[rows-1][j]=' ';
- ++i;
- int pos=0;
- char B[rows][keyLen];
- for(char c='A';c<='Z';++c)
- for(j=0;j<keyLen;++j)
- if(key[j]==c)
- {
- for(i=0;i<rows;++i)
- B[i][pos]=A[i][j];
- ++pos;
- }
- for(j=0; j<keyLen; ++j)
- {
- for(i=0;i<rows;++i)
- if(B[i][j]!=' ')
- fout<<B[i][j];
- fout<<' ';
- }
- }
- else
- {
- for(i=0;i<6;++i)
- for(j=0;j<6;++j)
- fin>>square[i][j];
- fin.get();
- char Key[770];
- fin>>Key;
- j=0;
- int rows=strlen(Key)+1;
- char B[rows][26];
- while(!fin.eof())
- {
- for(i=0;i<(int)strlen(Key);++i)
- B[i][j]=Key[i];
- if((int)strlen(Key)<rows)
- {
- Key[rows-1]=' ',Key[rows]='\0';
- B[rows-1][j]=' ';
- }
- ++j;
- fin.get();
- fin>>Key;
- }
- int NrLit=0,NrCol=--j;
- for(j=0; j<NrCol; ++j)
- if(B[rows-1][j]>='A' && B[rows-1][j]<='Z')
- ++NrLit;
- if(NrLit==0)
- --rows;
- else
- for(j=0;j<NrCol;++j)
- if(B[rows-1][j]<'A' || B[rows-1][j]>'Z')
- B[rows-1][j]=' ';
- int keyLen=strlen(Key);
- char A[rows][keyLen];
- int pos=0;
- for(char c='A';c<='Z';++c)
- for(j=0;j<keyLen;++j)
- if(Key[j]==c)
- {
- for(i=0;i<rows;++i)
- A[i][j]=B[i][pos];
- ++pos;
- }
- k=0;
- int nrsp=0;
- char cipher[rows*keyLen];
- for(i=0;i<rows;++i)
- for(j=0;j<keyLen;++j)
- if(A[i][j]!=' ')
- cipher[k++]=A[i][j];
- else
- ++nrsp;
- cipher[k]='\0';
- int Lin=0,Col=0;
- for(i=0; i<k; i+=2)
- {
- for(j=0;j<6;++j)
- {
- if(Index[j]==cipher[i])
- Lin=j;
- if(Index[j]==cipher[i+1])
- Col=j;
- }
- fout<<square[Lin][Col];
- }
- }
- return 0;
- }
- /**
- #include <fstream>
- using namespace std;
- string character[130],cheie,mesaj,criptmesaj,decriptmesaj,sir[10000];
- char invcharacter[2727];
- char matrix[1001][1001];
- int cer;
- int main()
- {
- ifstream cin("adfgvx.in");
- ofstream cout("adfgvx.out");
- cin>>cer;
- char ch;
- for(int i=1;i<=6;i++)
- for(int j=1;j<=6;j++)
- {
- cin>>ch;
- if(i==1)
- character[ch]+="A";
- else if(i==2)
- character[ch]+="D";
- else if(i==3)
- character[ch]+="F";
- else if(i==4)
- character[ch]+="G";
- else if(i==5)
- character[ch]+="V";
- else if(i==6)
- character[ch]+="X";
- if(j==1)
- character[ch]+="A";
- else if(j==2)
- character[ch]+="D";
- else if(j==3)
- character[ch]+="F";
- else if(j==4)
- character[ch]+="G";
- else if(j==5)
- character[ch]+="V";
- else if(j==6)
- character[ch]+="X";
- string cuv=character[ch];
- invcharacter[(cuv[0]-'A'+1)*100+cuv[1]-'A'+1]=ch;
- }
- if(cer==1)
- {
- cin>>mesaj>>cheie;
- for(int i=0;i<mesaj.size();i++)
- criptmesaj+=character[mesaj[i]];
- for(int i=0;i<criptmesaj.size();i++)
- {
- matrix[i/cheie.size()][i%cheie.size()]=criptmesaj[i];
- }
- for(char l='A';l<='Z';l++)
- {
- for(int j=0;j<cheie.size();j++)
- {
- if(cheie[j]==l)
- {
- for(int i=0;i<criptmesaj.size()/cheie.size()+1;i++)
- if(matrix[i][j]>='A' && matrix[i][j]<='Z')
- cout<<matrix[i][j];
- cout<<" ";
- }
- }
- }
- }
- else
- {
- int cnt=0;
- string s;
- while(cin>>s)
- sir[++cnt]=s;
- int poz=1,maxx=0;
- string length=sir[cnt];
- for(char ch='A';ch<='Z';ch++)
- {
- for(int i=0;i<length.size();i++)
- if(length[i]==ch)
- {
- string a=sir[poz];
- for(int k=0;k<a.size();k++)
- matrix[k][i]=a[k];
- poz++;
- int l=a.size();
- maxx=max(maxx,l);
- }
- }
- for(int i=0;i<maxx;i++)
- for(int j=0;j<length.size();j++)
- if(matrix[i][j]>='A' && matrix[i][j]<='Z')
- decriptmesaj+=matrix[i][j];
- for(int j=0;j<decriptmesaj.size();j+=2)
- cout<<invcharacter[(decriptmesaj[j]-'A'+1)*100+decriptmesaj[j+1]-'A'+1];
- }
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement