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

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.