492 - Pig-Latin

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

jfvs
New poster
Posts: 12
Joined: Wed Feb 02, 2011 10:40 am

Re: 492 - Pig Latin

Post by jfvs »

why do you output 10htay from the line 10th amr saqr? Im getting WA... and I think that my code is correct...

Code: Select all

import java.io.*;
import java.util.*;

public class p492 {
	static boolean is_vowel(char c){
		switch(c){
			case 'a': case 'e': case 'i': case 'o': case 'u': 
			case 'A': case 'E': case 'I': case 'O': case 'U':
				return true;
		}
		return false;
	}
	
	public static void main(String args[])throws IOException{
		Scanner e = new Scanner(System.in);
		String line;
		while(e.hasNext()){
			line = e.nextLine();
			char cline[] = line.toLowerCase().toCharArray(), 
			rline[] = line.toCharArray(), c = '\n';
			if(cline.length > 0) c = cline[0];
			boolean starts = (c >= 'a' && c <= 'z')? true : false, move = false, vowel = false;
			StringBuilder word = new StringBuilder();
			for(int i = 0; i < cline.length; i++){
				c = cline[i];
				if(!(c >= 'a' && c <= 'z')){
					String add = rline[i] + "";
					while(++i < rline.length && !((c = cline[i]) >= 'a' && c <= 'z'))
						add += rline[i] + "";
					i--;
					if(move){
						char cc = word.charAt(0);
						word = word.deleteCharAt(0);
						word.append(cc);
					}
					if(vowel) word.append("ay");
					word.append(add);
					System.out.print(word);
					word = new StringBuilder();
					starts = true; vowel = false; move = false;
				}else{
					if(starts){
						move = !is_vowel(c);
						starts = false;
					}
					vowel |= is_vowel(c);
					word.append(rline[i]);
				}
			}
			if(word.length() != 0){
				if(move){
					char cc = word.charAt(0);
					word = word.deleteCharAt(0);
					word.append(cc);
				}
				if(vowel) word.append("ay");
				System.out.print(word);
			}
			System.out.println();
		}
	}
}
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 492 - Pig Latin

Post by plamplam »

This is very important A "word'' is defined as a consecutive sequence of letters (upper and/or lower case). This means words can be separated by anything else other than alphabets. I solved this problem using getchar() as well as using gets(). If you use gets, your array size must be very huge(or else you will get Runtime Error). At first my char array size was 10000000 but I got Runtime error. Later, I declared this array globally and increased its size to 1000000000 and got Accepted easily. However, this problem is much easier and convenient to solve if getchar() is used. So I would recommend to use:

Code: Select all

while (scanf("%c", &ch)!= EOF)
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

Re: 492 - Pig Latin

Post by shaon_cse_cu08 »

I thing its a Clever call.... See the last word... It must be apended by a non-alphabetic character.... (space or . or something else)

Code: Select all

Input:
Amar nam shaon
Amar nam shaon.
10th
10th <--(space)
10th.
10th,
10th"

Output:
Amaray amnay haon
Amaray amnay haonsay.
10h
10htay 
10htay.
10htay,
10htay"

I'll keep holding on...Until the walls come tumbling down...And freedom is all around ..... :x
sonjbond
New poster
Posts: 19
Joined: Wed Jul 04, 2012 10:30 pm

why WA?(492)piglatin

Post by sonjbond »

my input output is pk ... bt y i am getting WA ??? plz help me ,,,, i m trying it from last 5 days but cant ,,,,, :( plz help ,,,
here's my code :

#include<stdio.h>
#include<string.h>
int main()
{
char str[1000000],wrd[1000000];
int len,n,i,j,k,l,m;
while(gets(str))
{
len=strlen(str);
m=0;
for(i=0; i<len+1; i++)
{
if((str>=65&&str<=90)||(str>=97&&str<=122))
{
wrd[m]=str;
m++;
}
else
{
if(wrd[0]=='A'||wrd[0]=='E'||wrd[0]=='I'||wrd[0]=='O'||wrd[0]=='U'||wrd[0]=='a'||wrd[0]=='e'||wrd[0]=='i'||wrd[0]=='o'||wrd[0]=='u')
{
for(j=0; j<=m-1; j++)
printf("%c",wrd[j]);
if(m!=0)
printf("ay");
}
else
{
if(m>1)
for(j=1; j<=m-1; j++)
printf("%c",wrd[j]);
if(m>=1)
printf("%c",wrd[0]);
if(m!=0)
printf("ay");
}
m=0;
printf("%c",str);
}
}
printf("\n");
}
return 0;
}


plz heip me ,,,, plz ... i can not try another problem b4 getting AC in 492 ,,,,,,,,,,, plz help me....
sonjbond
New poster
Posts: 19
Joined: Wed Jul 04, 2012 10:30 pm

Re: why WA?(492)piglatin

Post by sonjbond »

plz help .... reply my post plz....
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: why WA?(492)piglatin

Post by brianfry713 »

My AC code reads the input one character at a time, not a line at a time.
Check input and AC output for thousands of problems on uDebug!
sornaCse
New poster
Posts: 6
Joined: Thu Jul 26, 2012 9:40 am

492 why RE?

Post by sornaCse »

i am getting RE. Help me!

Code: Select all

#include<stdio.h>
#include<string.h>


int isVowel(char ch)
{
    int flag=0;
    if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
    flag=1;
    return flag;
}

void pigLatin(char str[10000])
{
    long i,len,flag,flag1,b=0;
    char ch;
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        flag=0;flag1=0;
        while(1)
        {
            if(str[i]==' ' || i == len)
            {
                if(flag==1)
                {
                    printf("%cay",ch);
                    if(i<len-1) printf(" ");
                    break;
                }
                else
                {
                    printf("ay");
                    if(i<len-1) printf(" ");
                    break;
                }
            }
            if(isVowel(str[i])==0 && flag1==0)
            {
                ch=str[i];
                flag1=1;
                flag=1;
            }
            else if(str[i]=='.') b=1;
            else
            {
                printf("%c",str[i]);
            }
            if(isVowel(str[i])==1 && flag1==0)
            {
                flag1=1;
            }
            i++;
        }
    }
    if(b==1)
    printf(".\n");else printf("\n");
}
 int main()
 {
     char str[10000];
     while(gets(str))
     {
        pigLatin(str);
     }
     return 0;
 }

Last edited by sornaCse on Wed Aug 29, 2012 8:23 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 492 why RE?

Post by brianfry713 »

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!
sornaCse
New poster
Posts: 6
Joined: Thu Jul 26, 2012 9:40 am

Re: 492 why RE?

Post by sornaCse »

brianfry713 wrote:Doesn't match the sample I/O.
I changed but RE!!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 492 why RE?

Post by brianfry713 »

Try reading a single character at a time instead of line by line.
Check input and AC output for thousands of problems on uDebug!
sonjbond
New poster
Posts: 19
Joined: Wed Jul 04, 2012 10:30 pm

Re: 492 why RE?? I/O is correct :S

Post by sonjbond »

why i m getting RTE ??
plz help me .........
my code is here :

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int isvowel(char ch)
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return 1;
else return 0;
}
int main()
{
int i,len,j,k;
char str[1000000];
while(gets(str))
{
len=strlen(str);
for(i=0;i<len;i++)
{
if(isalpha(str))
{
if(isvowel(str)==1)
{
printf("%c",str);
i++;
while(isalpha(str))
{
printf("%c",str);
i++;
}
printf("ay%c",str);
}
else
{
char temp=str;
i++;
while(isalpha(str))
{
printf("%c",str);
i++;
}
printf("%cay%c",temp,str);
}
}
else printf("%c",str[i]);

}
printf("\b\n");
}
return 0;
}



my another code by using getchar() got AC
bt why its not ???

plz help ..................
shipu_a
New poster
Posts: 23
Joined: Tue Oct 23, 2012 8:04 pm
Location: Dhaka,Bangladesh
Contact:

Re: 492 why RE?

Post by shipu_a »

WA plz help me................... :(

Code: Select all

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cctype>
#include<map>
#include<stack>
#include<cstdlib>
#include <queue>
#include <vector>
#include<algorithm>
#include<iostream>
#define ll long long
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
using namespace std;
char s[10000000],p[10000000];
int main()
{
    while(gets(s))
    {
        int j=0,k=0,f;
        for(int i=0;i<strlen(s);i++)
        {
            f=0;
            if(s[i]==' '||s[i]=='.')
            {
                if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'||
                   s[j]=='A'||s[j]=='E'||s[j]=='I'||s[j]=='O'||s[j]=='U')
                {
                    for(int b=j;b<=i;b++)
                    {
                        if(s[b]=='.')
                        {
                          f=1;
                          continue;
                        }
                        if(s[b]==' ')
                        continue;
                        p[k++]=s[b];
                    }
                    p[k++]='a';
                    p[k++]='y';
                    if(f==1)
                    {
                    p[k]='.';
                    f=0;
                    }
                    else
                    p[k++]=' ';

                    j=i+1;
                }
                else
                {
                   for(int b=j+1;b<=i;b++)
                   {
                       if(s[b]=='.')
                       {
                           f=1;
                           continue;
                       }
                       if(s[b]==' ')
                        continue;
                       p[k++]=s[b];
                   }
                    p[k++]=s[j];
                    p[k++]='a';
                    p[k++]='y';

                    if(f==1)
                    {
                    p[k]='.';
                    f=0;
                    }
                    else
                    p[k++]=' ';

                    j=i+1;
                }

            }
        }
       puts(p);
}
    return 0;
}
Nothing is imposible in the world.....And
Never Judge a Book by Its Cover.............
BUBT_Psycho
http://uhunt.felix-halim.net/id/168573
http://shipuahamed.blogspot.com
lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 492 why RE?

Post by lbv »

shipu_a wrote:WA plz help me................... :(
Check this input:

Input

Code: Select all

hello world

beep Beep Uu-pp blooo**
Output

Code: Select all

ellohay orldway

eepbay eepBay Uuay-ppay looobay**
A couple of additional comments:
  • Your code seems to assume that '.' (dot) and ' ' (space) are "special" characters to be tested for. Carefully read the problem statement again. Input could be classified as words and non-words. *Any* character that is not an uppercase or lowercase letter, is considered part of a non-word and must be left unchanged, and printed that way.
  • The strlen function takes time proportional to the length of the string; if you put it inside the condition of a loop, it will recalculate the size again and again, potentially slowing down your program significantly. Better save the result of strlen in a variable, and use that variable in the loop.
shipu_a
New poster
Posts: 23
Joined: Tue Oct 23, 2012 8:04 pm
Location: Dhaka,Bangladesh
Contact:

Re: 492 why RE?

Post by shipu_a »

lbv wrote:
shipu_a wrote:WA plz help me................... :(
Check this input:

Input

Code: Select all

hello world

beep Beep Uu-pp blooo**
Output

Code: Select all

ellohay orldway

eepbay eepBay Uuay-ppay looobay**
A couple of additional comments:
  • Your code seems to assume that '.' (dot) and ' ' (space) are "special" characters to be tested for. Carefully read the problem statement again. Input could be classified as words and non-words. *Any* character that is not an uppercase or lowercase letter, is considered part of a non-word and must be left unchanged, and printed that way.
  • The strlen function takes time proportional to the length of the string; if you put it inside the condition of a loop, it will recalculate the size again and again, potentially slowing down your program significantly. Better save the result of strlen in a variable, and use that variable in the loop.
now TLE

Code: Select all

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cctype>
#include<map>
#include<stack>
#include<cstdlib>
#include <queue>
#include <vector>
#include<algorithm>
#include<iostream>
#define ll long long
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
using namespace std;

int main()
{
    char s[2000000],c;
    while(gets(s))
    {
        for(int i=0;i<strlen(s);i++)
        {
            if(isalpha(s[i])!=0)
            {
                if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||
                   s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
                {
                    pf("%c",s[i]);
                    i++;
                    while(isalpha(s[i]))
                    {
                        pf("%c",s[i]);
                        i++;
                    }
                    i--;
                    pf("ay");
                }
                else
                {
                    c=s[i];
                    i++;
                    while(isalpha(s[i]))
                    {
                        pf("%c",s[i]);
                        i++;
                    }
                    i--;
                    pf("%c",c);
                    pf("ay");

                }
            }
            else
            pf("%c",s[i]);
        }
       pf("\n");
}
    return 0;
}
Nothing is imposible in the world.....And
Never Judge a Book by Its Cover.............
BUBT_Psycho
http://uhunt.felix-halim.net/id/168573
http://shipuahamed.blogspot.com
lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 492 why RE?

Post by lbv »

shipu_a wrote: now TLE
Did you read the second comment (the one about strlen)?

You can try changing this:

Code: Select all

          for(int i=0;i<strlen(s);i++)
for something like this:

Code: Select all

          for(int i=0, len = strlen(s);i<len;i++)
Post Reply

Return to “Volume 4 (400-499)”