Advertisement
a53

Scytale

a53
Sep 10th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char s[1000010];
  8. int c,m;
  9. ifstream f("scytale.in");
  10. f>>c>>s>>m;
  11. f.close();
  12. int n=(int)strlen(s);
  13. char A[m+m+1][n+m+1];
  14. for(int i=0;i<=m+m;++i)
  15. for(int j=0;j<=n+m;++j)
  16. A[i][j]='.';
  17. int k=0;
  18. ofstream g("scytale.out");
  19. if(s[0]<'A'||s[0]>'Z')
  20. {
  21. g.close();
  22. return 0;
  23. }
  24. if(c==1) /// Criptarea
  25. {
  26. for(int i=1;i<=m;++i) /// Formez matricea
  27. for(int j=i;j<=n;j+=m)
  28. A[i][j]=s[k++];
  29. for(int j=1;j<=n;) /// Afisez mesajul criptat
  30. for(int i=1;i<=m&&j<=n;++i,++j)
  31. if(A[i][j]!='.')
  32. g<<A[i][j];
  33. }
  34. else /// Decriptarea
  35. {
  36. for(int j=1;j<=n;) /// Formez matricea
  37. for(int i=1;i<=m;++i,++j)
  38. A[i][j]=s[k++];
  39. for(int i=1;i<=m;++i) /// Afisez mesajul decriptat
  40. for(int j=1;j<=n;++j)
  41. if(A[i][j]!='.')
  42. g<<A[i][j];
  43. }
  44. g<<'\n';
  45. g.close();
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement