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++:
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++:
- ///**********ALLAH IS ALMIGHTY************///
- ///AH Tonmoy
- ///Department of CSE
- ///Islamic University,Bangladesh
- #include<iostream>
- using namespace std;
- int main()
- {
- string s,s1;
- int n,i,c=0;
- cin>>n>>s;
- for(i=0; i<n; i++)
- {
- if((n-c)%2==1)
- s1=s1+s[i];
- else
- s1=s[i]+s1;
- c++;
- }
- cout<<s1<<endl;
- }
No comments