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

User avatar
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 » Fri Feb 15, 2013 8:57 pm

lbv wrote:
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++)
Thank you so much...................
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

walking_hell
New poster
Posts: 14
Joined: Tue Sep 24, 2013 4:35 pm

uva 492

Post by walking_hell » Sun Oct 20, 2013 10:07 am

cant find my bug

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char arr[1000000],word[1000000];
    int len,lenw,count,tm;
    while(scanf(" %[^\n]",arr)==1)
    {
        len=strlen(arr);
        for(count=0;count<len;count++)
        {

            if(tolower(arr[count])>='a' && tolower(arr[count])<='z')
            {

            sscanf(&arr[count]," %[A-Za-z]",word);
            lenw=strlen(word);
            count+=lenw;


                if(tolower(word[0])!='a' &&tolower(word[0])!='e'&&tolower(word[0])!='i'&&tolower(word[0])!='o'&&tolower(word[0])!='u')
            {
                for(tm=1;tm<lenw;tm++)
                printf("%c",word[tm]);
                printf("%cay",word[0]);

            }
            else
            printf("%say",word);



        }
        printf("%c",arr[count]);
        }
        printf("\n");

    }


    return 0;

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: uva 492

Post by brianfry713 » Mon Oct 21, 2013 9:33 pm

Try reading a char at a time.
Check input and AC output for thousands of problems on uDebug!

darksk4
New poster
Posts: 13
Joined: Sun Jul 29, 2012 7:10 pm

Re: why WA?(492)piglatin

Post by darksk4 » Mon Nov 25, 2013 4:15 pm

Pleace can you tell me why I am getting TLE in this code? :(

Code: Select all

Got AC thanks brianfry713 
Last edited by darksk4 on Tue Nov 26, 2013 6:43 pm, edited 1 time in total.

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 » Mon Nov 25, 2013 11:51 pm

cin, cout, and passing a string as an argument to a function are slow. Try using getchar() and putchar() instead.
Check input and AC output for thousands of problems on uDebug!

Shihab
New poster
Posts: 33
Joined: Thu Jun 13, 2013 1:19 pm

Re: 492 why RE?

Post by Shihab » Mon Dec 30, 2013 9:15 pm

TE , help

Code: Select all

#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
int main()
{
    char s,c;
    int i,j=0,k,flag=0,length,is_s_started,temp;
    while(1)
    {
        //c=getchar();
        scanf("%c",&c);
        if ((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') )
        {
            ++j;
            if((c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U') && j==1)
            {
                flag=0;//ckecking for vowel
                //putchar(c);
                printf("%c",c);
            }
            else
            {
                if(j==1)
                {
                    flag=1;
                    s=c;
                    //putchar(c);
                }
                else
                {
                    //putchar(c);
                    printf("%c",c);
                }
            }
        }
        else
        {

            if(flag==1)
            {
                flag=0;
                //putchar(s);
                printf("%c",s);

            }
            if(j>0)
            {
                //putchar('a' );
                //putchar('y');
                printf("ay");
            }
            //putchar(c);
            printf("%c",c);
            j=0;
        }
    }
    return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 492 why RE?

Post by brianfry713 » Thu Jan 16, 2014 12:35 am

Your code never terminates.
Check input and AC output for thousands of problems on uDebug!

Shihab
New poster
Posts: 33
Joined: Thu Jun 13, 2013 1:19 pm

Re: 492 why RE?

Post by Shihab » Sun Jan 19, 2014 11:41 am

Thanks, AC

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 492 why RE?

Post by uDebug » Mon Mar 10, 2014 9:36 am

This is a bit of a weird problem.

For starters, the judge's test seems rather weak. In my mind, if the word

Code: Select all

Apple
is on a line by itself, the AC output should be

Code: Select all

Appleay
However, UVA Toolkit outputs

Code: Select all

Apple
Also, I got a runtime error using

Code: Select all

while(getline(cin, aLine) 
where "aLine" is a std::string.

I ended up using

Code: Select all

while(scanf("%[^\n]%*c", aLine) != EOF)
where "aLine" is a char[] with size 1,000,000. There are more than 10,000 characters a line - so be warned. (I got a runtime error with char[] array with a size of 10,000).

That being said, here's some input / output I found useful.

Input:

Code: Select all

Apple
This
;'/.,][                 -()*&^%$#@!
 A a            E       e o Oo U u i                            I
   The quIck brOwn fOx jumps OVER the LAzy doG       .
AC Output:

Code: Select all

Appleay
hisTay
;'/.,][                 -()*&^%$#@!
 Aay aay            Eay       eay oay Ooay Uay uay iay                            Iay
   heTay uIckqay rOwnbay Oxfay umpsjay OVERay hetay AzyLay oGday       .
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

jddantes
Learning poster
Posts: 73
Joined: Sat Mar 08, 2014 8:55 am

Re: 492 why RE?

Post by jddantes » Wed May 21, 2014 12:14 pm

Why RE?

Code: Select all

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

void strapp(char * dest, char * src);
void processWord(char * word);
int isVowel(char c);

int main(){
	
	char raw[10000000];

	while(fgets(raw,10000000,stdin)!=NULL){

		//printf("Got input: %s", raw);

		if(raw[strlen(raw)-1]=='\n')
			raw[strlen(raw)-1] = 0;

		char decoded[10000000] = {};
		//printf("Decoded: %s\n",decoded);

		char word[100000];
		memset(word,0,100000);

		int i = 0;
		int wordindex = 0;

		for(i=0; 1;i++){
			if(!isalpha(raw[i])){
				// Process word
				processWord(word);
				//printf("Word is now %s\n",word);
				strapp(decoded, word);
				//printf("Decoded:%s\n",decoded);
				if(raw[i]==0)
					break;

				//Append this character
				char cword[5];
				cword[0]=raw[i];
				cword[1]=0;
				strapp(decoded,cword);

				wordindex = 0;
				memset(word,0,100000);

				//printf("Decoded2: %s\n",decoded);


			} else {
				word[wordindex] = raw[i];
				word[wordindex+1] = 0;
				//puts(word);
				wordindex++;
			}
		}
		//printf("Processed string: %s\n",decoded);
		printf("%s\n",decoded);

	}



	return 0;
}

void strapp(char * dest, char * src){
	int len = strlen(dest);
	strcpy(dest+len,src);
}

void processWord(char * word){

	if(word[0]==0)
		return;

	if(isVowel(word[0])){
		//printf("Vowel\n");
		strapp(word,"ay");
		return;
	} else {
		char temp[100000];
		strcpy(temp,word+1);
		char t[5];
		t[0]=word[0];
		t[1]=0;
		strapp(temp,t);
		strapp(temp, "ay");
		strcpy(word,temp);
	}
	//printf("done: %s\n",word);
}

int isVowel(char c){
	char x = tolower(c);
	if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u'){
		//printf("%c is a vowel\n",c);
		return 1;
	}
	//printf("%c is not a vowel\n",c);
	return 0;
}

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 492 why RE?

Post by uDebug » Wed May 21, 2014 12:25 pm

jddantes wrote:Why RE?
10,000,000 bytes is ~9.5 MB and the stack has only ~1 MB. If you really need that much place, try making the array of characters global in scope.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

jddantes
Learning poster
Posts: 73
Joined: Sat Mar 08, 2014 8:55 am

Re: 492 why RE?

Post by jddantes » Wed May 21, 2014 7:43 pm

Okay thanks for that. I got TLE now though. Is strapp causing this? (It uses strlen)

Code: Select all

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

void strapp(char * dest, char * src);
void processWord(char * word);
int isVowel(char c);

char raw[1000000];
char decoded[1000000];


int main(){

	memset(raw, 0, 1000000);

	while(fgets(raw,1000000,stdin)!=NULL){

		//printf("Got input: %s", raw);


		if(raw[strlen(raw)-1]=='\n')
			raw[strlen(raw)-1] = 0;

		memset(decoded, 0, 1000000);
		//printf("Decoded: %s\n",decoded);

		char word[100000] = {};

		int i = 0;
		int wordindex = 0;
		for(i=0; 1;i++){
			if(!isalpha(raw[i])){
				// Process word
				processWord(word);
				//printf("Word is now %s\n",word);
				strapp(decoded, word);
				//printf("Decoded:%s\n",decoded);
				if(raw[i]==0)
					break;

				//Append this character
				char cword[5];
				cword[0]=raw[i];
				cword[1]=0;
				strapp(decoded,cword);

				wordindex = 0;
				memset(word,0,100000);

				//printf("Decoded2: %s\n",decoded);


			} else {
				word[wordindex] = raw[i];
				word[wordindex+1] = 0;
				//puts(word);
				wordindex++;
			}
		}
		//printf("Processed string: %s\n",decoded);
		printf("%s\n",decoded);
		memset(raw, 0, 1000000);
	}



	return 0;
}

void strapp(char * dest, char * src){
	int len = strlen(dest);
	strcpy(dest+len,src);
}

void processWord(char * word){

	if(word[0]==0)
		return;

	if(isVowel(word[0])){
		//printf("Vowel\n");
		strapp(word,"ay");
		return;
	} else {
		char temp[100000];
		strcpy(temp,word+1);
		char t[5];
		t[0]=word[0];
		t[1]=0;
		strapp(temp,t);
		strapp(temp, "ay");
		strcpy(word,temp);
	}
	//printf("done: %s\n",word);
}

int isVowel(char c){
	char x = tolower(c);
	if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u'){
		//printf("%c is a vowel\n",c);
		return 1;
	}
	//printf("%c is not a vowel\n",c);
	return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 492 why RE?

Post by brianfry713 » Wed May 21, 2014 8:22 pm

Do you need to use memset on every line?
Check input and AC output for thousands of problems on uDebug!

sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 492 why RE?

Post by sampad74 » Mon Jul 21, 2014 10:27 am

I am getting RE.Here is my code.Please anybody help..Thanks in advance.

Code: Select all

got AC
Last edited by sampad74 on Tue Jul 22, 2014 4:59 am, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 492 why RE?

Post by lighted » Mon Jul 21, 2014 12:39 pm

Increase parameter of array

Code: Select all

char a[100000];
It must be

Code: Select all

char a[1000000];
You will have TLE.

Try to change

Code: Select all

for(j=0;a[j];j++)
You can do it like this

Code: Select all

#include<stdio.h>
#include<string.h>
..
..
int l = strlen(a);
for(j = 0; j < l; j++)
Now you can remove this. It is reason why you will have TLE.

Code: Select all

for(i=0;i<100000;i++) a[i]='\0';
You will have OLE. Your program will run with infinite loop

Code: Select all

while(1)
    {
        gets(a);
        ..
        ..
    }
When will your program end? At which condition?
Read posts in this thread first
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Post Reply

Return to “Volume 4 (400-499)”