Page 13 of 15

Re: 492 - Pig Latin

Posted: Fri May 27, 2011 6:36 am
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();
		}
	}
}

Re: 492 - Pig Latin

Posted: Fri Jul 22, 2011 1:13 pm
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)

Re: 492 - Pig Latin

Posted: Thu Aug 25, 2011 9:26 pm
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"


why WA?(492)piglatin

Posted: Mon Jul 09, 2012 4:48 am
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....

Re: why WA?(492)piglatin

Posted: Sun Jul 15, 2012 11:44 pm
by sonjbond
plz help .... reply my post plz....

Re: why WA?(492)piglatin

Posted: Tue Jul 17, 2012 12:47 am
by brianfry713
My AC code reads the input one character at a time, not a line at a time.

492 why RE?

Posted: Sun Jul 29, 2012 8:44 pm
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;
 }


Re: 492 why RE?

Posted: Mon Jul 30, 2012 11:29 pm
by brianfry713
Doesn't match the sample I/O.

Re: 492 why RE?

Posted: Wed Aug 29, 2012 8:24 pm
by sornaCse
brianfry713 wrote:Doesn't match the sample I/O.
I changed but RE!!

Re: 492 why RE?

Posted: Wed Aug 29, 2012 11:03 pm
by brianfry713
Try reading a single character at a time instead of line by line.

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

Posted: Tue Sep 11, 2012 9:22 pm
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 ..................

Re: 492 why RE?

Posted: Fri Feb 15, 2013 12:18 pm
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;
}

Re: 492 why RE?

Posted: Fri Feb 15, 2013 4:35 pm
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.

Re: 492 why RE?

Posted: Fri Feb 15, 2013 7:28 pm
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;
}

Re: 492 why RE?

Posted: Fri Feb 15, 2013 8:33 pm
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++)