×

# ENCMSG- Editorial

Tester- Ivan Safonov
Editorialist- Abhishek Pandey

CAKEWALK

# PROBLEM:

Given a string $S$ on $N$ characters, we have to perform the following operations-

• swap adjacent characters. Eg- Swap $1$ with $2$, $3$ with $4$ and so on.
• swap every alphabet OF THE ORIGINAL STRING with its mirror reverse, i.e. if ASCII value of the character is $K$ , swap it with character of ASCII value $'a'+('z'-K)$. Hence, $'a'$ is replaced by $'z'$, $'b'$ with $'y'$ and so on.

# QUICK EXPLANATION:

Key to AC- This is a majorly implementation question. Proper practice in these topics is the key to quick AC.

Swapping of the adjacent characters is trivial. The replacement of alphabets can be easily done by realizing that every alphabet is replaced by $'a'+('z'-K)$ counterpart, where $K$ is ASCII value of the character.

# EXPLANATION:

This editorial will have only a single section, as its majorly an easy implementation problem. We will be discussing setter's solution owing to its neat implementation.

1.Setter's solution-

The first thing to carry out is the swap operation. Remember the kind of swapping. Let me denote character at index $i$ with $c_i$. We have to swap $c_0$ with $c_1$, $c_2$ with $c_3$ and so on. Familiarity with arrays and strings is needed. You can use a temporary variable for swapping, or perhaps put std::swap function from C++ STL. Both the ways are given in tab below-

View Content

Now all that remains is to do the alphabet replacing. We can do that trivially using the formula given above. An example is given under tab. We will replace $s[i]$ with $s[i]='a'+('z'-s[i]);$

View Content

# SOLUTION:

The codes of setter and tester are pasted below in tabs for you guys to refer if the links dont work. @admin needs some time for linking solutions, hence code was pasted for your convenience.

Setter

View Content

Tester

View Content

$Time$ $Complexity=$ $O(N)$

# CHEF VIJJU'S CORNER :D

1. Some contestants wrote a logic similar to below while coding for swapping part-

for(int i=0;i< n-1;i++){ char c=s[i]; s[i] = s[i+1]; s[i+1] = c; }

Is something wrong there? :o

2. This time, we were able to derive a formula. What if, this wouldnt be possible? What if mapping would had been random, eg - Replace 'a' with 'g', 'h' with 'i', 'b' with also 'g' , or perhaps what if mapping would be given in input? Its easy still! Just make an array of size $26$ and store which alphabet is $i'th$ one mapped to. Meaning, $table[i]$ would store the character to which alphabet with ASCII $'a'+i$ gets mapped to. Now, another version, what if I say that the characters are upto ${10}^{9}$, but only ${10}^{5}$ are used and need to be mapped? I want to bring std::map data structure to your attention. :)

This question is marked "community wiki".

15.2k11859
accept rate: 18%

 1 @imvarnit_1996 you can't use void main(). You have to use int main() with return statement at end. taking character array of length 100 is wrong because in the end there is null character of each string and any string of length 100 wouldn't fit in the array. for(j=N;j>=0;j++) is wrong. Nth character would be NULL. I don't know what logic you used because of poor readability of code but I hope this helps. answered 27 Jul '18, 00:10 65●7 accept rate: 8%
 0 Hi everyone, I have a curiosity here. Instead of swapping the values, can't we dynamically print the output? Here a sample code to explain my point. TestCases on IDE runs well but at the end while submitting I get WA. Any help will be great. Language:C int main() { int T; scanf("%d",&T); do{ int B; scanf("%d",&B); char str[B]; scanf("%s",str); for(int i=0; i< ((B/2)*2); i++){ if(i%2==0){ printf("%c",219 - str[i+1]); }else{ printf("%c",219 - str[i-1]); } } if(B%2 != 0) printf("%c\n",219 - str[B-1]); }while(T-->1); return 0; }  answered 23 Jul '18, 08:17 1 accept rate: 0% Yes, instead of swapping you can do it directly as well. Take two characters every iteration, print the second character's corresponding alphabet first, then do same for first one. (23 Jul '18, 14:48)

# include <string.h>

void main(){ char S[100] int T scanf("%d",&T); scanf("%d",&N); gets(S); if (N%2==0) { for(j=N;j>=0;j--){ str[j]=str[j--]; goto nextstep; exit(); } } else { nextstep: for (int i = 0; i < N; i++) { temp=str[i]; str[i]=str[i+1]; str[i+1]=temp; break;
} for (int k = 0; k < N; k++) { str[k] = 'z' + 'a'- str[k]; } } printf("%s",str); }

can anyone help...

1
accept rate: 0%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×814
×630
×96

question asked: 20 Jul '18, 19:32

question was seen: 711 times

last updated: 27 Jul '18, 00:10