Codeforces 746B. Decoding Solution

Hints:
Here, it is important to note that encoding and decoding are just inverse operations of each other similar to e^x and log(x). We take the algorithm that is used to encode in order to decode. We loop through each element in the input, which we store as a character array, and then keep a counter (mid+-counter)of the position of where this element should be in the decoded string. If the input is even length, then we want to go right first, and if the input is odd length, then we want to go left first.

Solve in C++:

  1. ///**********ALLAH IS ALMIGHTY************///
  2. ///AH Tonmoy
  3. ///Department of CSE
  4. ///Islamic University,Bangladesh
  5. #include<iostream>
  6. using namespace std;
  7. int main()
  8. {
  9. string s,s1;
  10. int n,i,c=0;
  11. cin>>n>>s;
  12. for(i=0; i<n; i++)
  13. {
  14. if((n-c)%2==1)
  15. s1=s1+s[i];
  16. else
  17. s1=s[i]+s1;
  18. c++;
  19. }
  20. cout<<s1<<endl;
  21. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.