Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstring>
- using namespace std;
- int F[26];
- struct LC
- {
- int x,y;
- };
- LC lc[26];
- int main()
- {
- int c;
- ifstream f("playfair.in");
- f>>c;
- char s[5][5],t[10000];
- for(int i=0;i<5;++i)
- for(int j=0;j<5;++j)
- f>>s[i][j],F[s[i][j]-'a']=1,lc[s[i][j]-'a'].x=i,lc[s[i][j]-'a'].y=j;
- f>>t;
- char lit;
- for(int i=0;i<26;++i) /// Stabilim litera care nu exista in matrice
- if(F[i]==0)
- lit=i+'a',i=27;
- int L=(int)strlen(t);
- if(c==1)
- for(int i=0;i<L;i+=2)
- if(t[i]==t[i+1])
- t[i+1]=lit;
- else
- {
- int a=lc[t[i]-'a'].x,b=lc[t[i]-'a'].y,c=lc[t[i+1]-'a'].x,d=lc[t[i+1]-'a'].y;
- if(a!=c&&b!=d)
- t[i]=s[a][d],t[i+1]=s[c][b];
- if(a==c)
- {
- if(b==4)
- b=-1;
- if(d==4)
- d=-1;
- t[i]=s[a][b+1],t[i+1]=s[c][d+1];
- }
- if(b==d)
- {
- if(a==4)
- a=-1;
- if(c==4)
- c=-1;
- t[i]=s[a+1][b],t[i+1]=s[c+1][d];
- }
- }
- else
- for(int i=0;i<L;i+=2)
- if(t[i+1]==lit)
- t[i+1]=t[i];
- else
- {
- int a=lc[t[i]-'a'].x,b=lc[t[i]-'a'].y,c=lc[t[i+1]-'a'].x,d=lc[t[i+1]-'a'].y;
- if(a!=c&&b!=d)
- t[i]=s[a][d],t[i+1]=s[c][b];
- if(a==c)
- {
- if(b==0)
- b=5;
- if(d==0)
- d=5;
- t[i]=s[a][b-1],t[i+1]=s[c][d-1];
- }
- if(b==d)
- {
- if(a==0)
- a=5;
- if(c==0)
- c=5;
- t[i]=s[a-1][b],t[i+1]=s[c-1][d];
- }
- }
- ofstream g("playfair.out");
- g<<t<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement