TOTR - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

CAKEWALK

PREREQUISITES

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.

4 Likes

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 CodeChef: Practical coding for everyone

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

link:Redirecting...

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

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] :slight_smile: Otherwise you end up corrupting some other array (and don’t get any memory access errors… just WA because of the corruption)

I guess this can be easily done with the help of ASCII table. Following is the code in JAVA.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    if (scanner.hasNextLine()) {
        String[] line = scanner.nextLine().split(" ");
        int testCase = Integer.parseInt(line[0]);
        String permutation = line[1];
        String permutationC = line[1].toUpperCase();
        while (testCase-- > 0) {
            String toBt = scanner.nextLine();
            StringBuilder sb = new StringBuilder();
            for (char c : toBt.toCharArray()) {
                int val = ((int)c);
                if (c == '_') {
                    sb.append(" ");
                } else if (val > 64 && val < 91) {
                    // upperCase
                    sb.append(permutationC.charAt(val - 65));
                } else if (val > 96 && val < 123) {
                    // lowercase
                    sb.append(permutation.charAt(val - 97));
                } else {
                    sb.append(c);
                }
            }
            System.out.println(sb);
        }
    }
}

Could someone please explain what does A* represent and can it be used in a real code or is it just a pseudocode term.

can anyone tell me what this statement means…
if((str[i]-‘A’)>25)
{

I’m getting a run time error, can someone tell me the problem please??
It’s showing the error in the second line near the input which works fine while given custom input.
Here’s the link:
https://www.codechef.com/viewsolution/30510982

Hey can someone help me with this solution, its running fine on my system but is showing wrong answer when i’m trying to submit. CodeChef: Practical coding for everyone

Can someone look into my code its shoving nothing:
code:
#include<bits/stdc++.h>
#define shadow ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
using namespace std;
int main()
{

int t,x;
cin>>t;
map<int,char> alpha;
map<int,char>::iterator itr;
char ch;
for(int i=1;i<=26;i++)
{
	cin>>ch;
    alpha.insert(pair<int,char>(i,ch));
}
/*for (itr = alpha.begin(); itr != alpha.end(); ++itr) 
{ 
    cout << '\t' << itr->first 
         << '\t' << itr->second << '\n'; 
     }
     */
while(t--)
{
	string s;
	cin>>s;
	vector<char> res;
	for(int i=0;i<s.length();i++)
	{
		ch=s[i];
		if(ch=='_')
		res.push_back(' ');
		else
		{
			if(isupper(ch))
			{
				ch=tolower(ch);
			}
			x=int(ch);
			res.push_back(alpha[x]);
		}
	}
	for(int i=0;i<res.size();i++)
	cout<<res[i];
	cout<<endl;
}

}

output:
5 qwertyuiopasdfghjklzxcvbnm
Ph

Pcssi

Bpke_kdc_epclc_jcijsc_mihyo?

Epcf_kdc_liswhyo_EIED_hy_Vimcvpcn_Zkdvp_siyo_viyecle.

Ipp!

nothing is coming in the output.i think i tried my best