# TOTR - Editorial

CAKEWALK

Ad Hoc

### PROBLEM

The problem statement of Tourist Translations boils down to the following points

• You are given a mapping of the 26 english alphabets onto itself. (Let this be called map)
• Transform a string using this mapping while maitaining the case.
• Preserve punctuation.
• Replace ‘_’ with single space.

### EXPLANATION

For those new to programming, solving this problem should lead to discovery of how to map a character in the english alphabet to another. map is input in the form of a string of 26 characters, which contains a permutation of the characters.

• The first character is the what ‘a’ maps to.
• The second character is what ‘b’ maps to.
• The 26th character is what ‘z’ maps to.

This can be achieved by mapping characters in english to indexes in map, and then use the character at that index. Given a string A of english alphabets, this can be done as following

```for i = 1 to length(A)
A[i] = map[A[i] - 'a']
```

Of course, this is possible because characters in the ASCII charset are stored in a continuous block that starts at 65, which is ‘a’. The above snippet has one bug right now. We are not detecting the character case and trying to maintain it. Let us fix that.

```for i = 1 to length(A)
if A[i] = lower-case-alphabet
A[i] = lower-case( map[A[i] - 'a'] )
if A[i] = upper-case-alphabet
A[i] = upper-case( map[A[i] - 'a'] )
```

Now, all that is needed is to handle the other two types of characters - punctuation and underscores.

```for i = 1 to length(A)
if A[i] = underscore
A[i] = space
if A[i] = lower-case-alphabet
A[i] = lower-case( map[A[i] - 'a'] )
if A[i] = upper-case-alphabet
A[i] = upper-case( map[A[i] - 'a'] )
/* Ignore punctuation */
```

Both the Setter’s and Tester’s solution use the approach described above.

### SETTER’S SOLUTION

Can be found here.

### TESTER’S SOLUTION

Can be found here.

3 Likes

I might sound stupid but i can’t really find out whats wrong with this program…
do take a look…
http://www.codechef.com/viewsolution/1885818

@h1t35h you should use a dummy gets() before scanning the string in order to read the new line character.

@h1t35h map[26] should be map[27] Otherwise you end up corrupting some other array (and don’t get any memory access errors… just WA because of the corruption)

Same problem with me too, am getting WA but my program runs fine on my PC, Here is it link

thanks.

Hello @avonnadozie the solution seems correct but your program is not running all test cases in some situation because of this:

``````    Scanner input = new Scanner(System.in);
char[] firstChars;

firstChars = input.nextLine().toCharArray();
lenght = firstChars[0] - 48;
``````

It seems like you’re using you variable length to store the length of the conversation or the number of test cases but you’re assuming that only the first char contains the number of test cases and that is not true for all inputs… Since the string following the number of test cases contains only letters you should loop the firstChars array and keep adding the length value until the space character is found , and then read the string. And avoid using Scanner and System.out.println(), use BufferedReader and BufferedWriter instead, but I would recommend you to try and run your program as it is (I think for this problem Scanner and System.out.println will be fast enough since the number of test cases is at most 100). Then you can focus on your reading and writing methods. Besides BufferedReader is easier to use than Scanner, at least for me. See Why I get TLE and Java Wiki for better understanding.

1 Like

Yes, i see it now. thanks alot

Getting WA.please help
http://www.codechef.com/viewsolution/5256818

1 Like

can anybody tell me error in this code http://www.codechef.com/viewsolution/5836052

the problem with my code is that after entering a sentence containing punctuation marks and a sentence with only alphabets…the output comes out containing some letters from previous output…otherwise the output will be correct…can anyone tell me what’s wrong here…thanks!!

#include<stdio.h>
int main()
{
int t;
char a[26];
int b[26];
char c[100];
int k,j,i=0;
scanf("%d",&t);
scanf(" “);
while((a[i++]=getchar())!=’\n’);
i=0;
j=0;
while(i<26)
{
b[i]=97+i;
i++;
}
a[i]=’\0’;
while(t–)
{
j=0;
i=0;
while((c[i++]=getchar())!=’\n’)
c[i]=’\0’;
i=0;
while(c[i]!=’\0’)
{
for(j=0;j<26;j++)
{
if(c[i]==’_’)
{
printf(” “);
break;
}
else if(c[i]==b[j] || c[i]==b[j]-32)
{
if(c[i] >=‘A’ && c[i] <= ‘Z’)
{
printf(”%c",a[j]-32);
break;
}
else if(c[i] >=‘a’ && c[i] <=‘z’)
{
printf("%c",a[j]);
break;
}
}
else if(c[i]==’.’ || c[i]==’,’ || c[i]==’!’ || c[i]==’?’)
{
printf("%c",c[i]);
break;
}
fflush(stdin);
fflush(stdout);
}
i++;
}
if(t>=1)
{
printf("\n");
}
}
return 0;
}

if you have any doubts related to any of your answers and want to LEARN and SHARE good tricks and concepts in coding.join our coding community and let us all coders rock

my code is giving run time error.please help
https://www.codechef.com/viewsolution/15497034

Hello my code’s execution time is 0.03 how can i minimise this? some have 0.02!! help please… where am i missing 0.01?
https://www.codechef.com/viewsolution/16622777