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

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

for(i=0;i<=strlen(str);i++)
The check "i<=strlen(str)" in this part of your code is done at each loop iteration, and every time strlen() is recomputed again and again, resulting in quadratic runtime.

Get rid of strlen() in the loop condition, e.g. by computing strlen() just once, before the loop begins, or checking that str != 0.

Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

Post by Fuad Hassan EWU »

:D thanks mf. i got AC after having you as help line.

after changing my code according to your direction i was getting RTE.
then making the arrays static i got AC.

thanks.
Eagle er moto daana meley urbo

sumonSUST
New poster
Posts: 5
Joined: Fri Oct 26, 2007 3:55 pm

Post by sumonSUST »

Code: Select all


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define SI 1000009

char line[SI];

int main()
{
	
	long i,j,k,len;

    while(gets(line))  
	{
		len=strlen(line);
		
		for(i=0;i<len;i++)
		{
			if(toupper(line[i])>='A' && toupper(line[i])<='Z')
			{
				if(toupper(line[i])=='A'||toupper(line[i])=='E'||toupper(line[i])=='I'||toupper(line[i])=='O'||toupper(line[i])=='U')
				{
					while(line[i]!=' ' && i<len)
					{
						if(toupper(line[i])>='A' && toupper(line[i])<='Z')
						{
							printf("%c",line[i]);
						}
						else
						{
							break;
						}
						i++;
					}
					printf("%s%c","ay",line[i]);
				}
				else
				{
					j=i;
					
					while(line[++i]!=' ' && i<len)
					{
						if(toupper(line[i])>='A' && toupper(line[i])<='Z')
							printf("%c",line[i]);
						else
						{
							break;
						}
						
					}
					printf("%c%s%c",line[j],"ay",line[i]);
				}
			}
			else
				printf("%c",line[i]);
		}
		printf("\n");
	}		
	
	
	return 0;
}
i get runtime error
plz help me.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

I don't know why you are getting RTE but I think your code is not correct. Suppose the input is only 'b'. Your code prints the null character, too. Save your output in a file. Then check it.
Ami ekhono shopno dekhi...
HomePage

yangmie
New poster
Posts: 3
Joined: Fri Nov 30, 2007 12:14 pm
Contact:

Post by yangmie »

Code: Select all

#include<stdio.h>
#include<ctype.h>
char input[1000000];
char temp[1000000];
int main()
{
        while(gets(input)!=NULL)
        {
                int i=0;
                while(input[i]!='\0')
                {
                        int x=0;
                        while(isalpha(input[i]))
                        {
                                temp[x]=input[i];
                                x++;
                                i++;
                        }
                        int y;
                        if(temp[0]=='a'||temp[0]=='e'||temp[0]=='i'||temp[0]=='o'||temp[0]=='u'||temp[0]=='A'||temp[0]=='E'||temp[0]=='I'||temp[0]=='O'||temp[0]=='U')
                        {
                                for(y=0;y<x;y++)
                                        printf("%c",temp[y]);
                                printf("ay");
                        }
                        else if(isalpha(temp[0]))
                        {
                                for(y=1;y<x;y++)
                                        printf("%c",temp[y]);
                                printf("%cay",temp[0]);

                        }
                        printf("%c",input[i]);
                        i++;
                        temp[0]='\0';
                }
                printf("\n");
                int k=0;
                while(input[k]!='\0')
                {
                        input[k]='\0';
                        k++;
                }
        }
        return 0;
}
why wa.....

[Edited by : Jan] Use code tags..

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Save your output to a file. You are printing the terminating NULL character, too.

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am

Post by AcmNightivy »

i got WA,RE,TLE for three simple problems for more than one page..my god..And now i can't find my mistakes in this problem..All the cases I found run correctly..Help..

Code: Select all

#include <stdio.h>

int main ()
{
	char str[12000];
	char word[10000];
	int i, j;
	int current;

	while (gets (str))
	{
		current = 0;
		while (str[current] >= 'z' || str[current] <= 'A')
		{
			if (str[current] == '\0')
			{
				break;
			}
			printf ("%c", str[current++]);
		}

		if (str[current] == '\0')
		{
			printf ("\n");
			continue;
		}

		while (true)
		{		
			j = 0;
			while (str[current] <= 'z' && str[current] >= 'A')
			{
				word[j++] = str[current++];
			}
			word[j] = '\0';

			if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u' 
			 || word[0] == 'A' || word[0] == 'E' || word[0] == 'I' || word[0] == 'O' || word[0] == 'U')
				printf ("%say", word);
			else
			{
				for (i = 1; word[i] != '\0'; i++)
					printf ("%c", word[i]);
				printf ("%cay", word[0]);
			}

			while (str[current] <= 'A' || str[current] >= 'z')
			{
				if (str[current] == '\0')
					break;
				printf ("%c", str[current]);
				current++;
			}
			if (str[current] == '\0')
			{
				printf ("\n");
				break;
			}
		}	
	}
	return 0;
}

turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
Location: (CSE,DU) Dhaka,Bangladesh

Post by turcse143 »

Code: Select all

while (gets (str))
u don't take input into array. it cause TLE.
instead of it,u can take input by

Code: Select all

while(scanf("%c")==1)
then arrange ur code
hope all the error will solved
''I want to be most laziest person in the world''

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am

Post by AcmNightivy »

turcse143 wrote:

Code: Select all

while (gets (str))
u don't take input into array. it cause TLE.
instead of it,u can take input by

Code: Select all

while(scanf("%c")==1)
then arrange ur code
hope all the error will solved
why i can't using gets()?Thanks~

rajib_cse_sust
New poster
Posts: 2
Joined: Mon May 05, 2008 11:05 am
Location: sylhet,Bangladesh

492 :: WA Wa help about Wa why?

Post by rajib_cse_sust »

Code: Select all


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define  MAX 10000000 

char str[MAX];
int isvowel(char  latter);

int main()
{
	long  i, len, flag;
	char  temp;
	
//	freopen("E:\\nikson\\in.txt","r",stdin);

	while( gets(str) )
	{
		len = strlen( str );
		
		for( i = 0; i < len; i++ )
		{
			if( isspace( str[i] ) || ispunct( str[i] ) )		printf("%c", str[i]);
		
			else
			{
				flag = isvowel( str[i] );
				
				if( flag )
				{
					while(1)
					{
						if( isspace( str[i] ) || ispunct( str[i] ) || ( i == len ) )	{ --i; break; }
						else	printf("%c", str[ i++ ]);
					}

					printf("ay");
				}
				else 
				{
					temp = str[ i++ ];

					while( 1 )
					{
						if( isspace( str[i] ) || ispunct( str[i] ) || ( i == len ) )	{ --i; break; }
						else	printf("%c", str[ i++ ]);
					}
					
					printf("%cay", temp);
				}
			}
		}

		if( str[len] != '\0')
			printf("%c", str[len]);
	}
	return 0;
}


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



Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 492 :: WA Wa help about Wa why?

Post by Jan »

I have already told you to use existing thread.
Ami ekhono shopno dekhi...
HomePage

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

Re: 492 WA: tell me why ........................

Post by amr saqr »

"non-words should be output exactly as they appear in the input" & A word is defined as "a consecutive sequence of letters",
Simply, the previous line contains the tricks of this problem,
any character outside the range 'a'-'z' or the range 'A'-'Z' is a non-word,
manipulate words only :) ,

and here is some critical inputs/outpus 4 u,

input:

Code: Select all

This is the input.
He is a boy.

Object123oriented,..,.Programming
000000000
aaaaaaaaaaaa
Aaaaaaaaaaaa
computerprogrammingII
cse-1204 Computer programming II
ACM programming contest was held on 10th November, 2001 at BUET.
an a eye An A Eye Bee BEE Dhaka-1205 1st last.
output:

Code: Select all

 hisTay isay hetay inputay.
eHay isay aay oybay.

Objectay123orienteday,..,.rogrammingPay
000000000
aaaaaaaaaaaaay
Aaaaaaaaaaaaay
omputerprogrammingIIcay
secay-1204 omputerCay rogrammingpay IIay
ACMay rogrammingpay ontestcay asway eldhay onay 10htay ovemberNay, 2001 atay UETBay.
anay aay eyeay Anay Aay Eyeay eeBay EEBay hakaDay-1205 1tsay astlay.
C++ Is The Best.

vinocit
New poster
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

WA in 492. Why?

Post by vinocit »

Could any one say whats wrong in my code i am getting WA

Code: Select all

#include<iostream>
using namespace std;
int isvowel(char c)
{
	switch(c)
	{
		case 'a':
		case 'A':
		case 'e':
		case 'E':
		case 'i':
		case 'I':
		case 'o':
		case 'O':
		case 'u':
		case 'U':
			return 1;
	}
	return 0;
}
int main()
{
	char s[1000];
	int n=0;
	while(cin.getline(s,1000))
	{
		int l=strlen(s);
		int pos=0;
		if(n)cout<<"\n";
		n++;
		while(s[pos]!=0)
		{
			char start=0;
			while(!((s[pos]>='a'&&s[pos]<='z')||(s[pos]>='A'&&s[pos]<='Z')||pos>=l))
			{
				cout<<s[pos];
				pos++;
			}
			if(s[pos]==0)break;
			if(!(isvowel(s[pos])))
			{
				start=s[pos];
				pos++;
			}
			while(((s[pos]>='a'&&s[pos]<='z')||(s[pos]>='A'&&s[pos]<='Z'))&&pos<l)
			{
				cout<<s[pos];
				pos++;
			}
			if(start)cout<<start;
			cout<<"ay";
		}
	}
	return 0;
}

vinocit
New poster
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

Re: 492 WA: tell me why ........................

Post by vinocit »

Could any one say wats wrong with my code. My code gives correct output with the above input

Code: Select all

#include<iostream>
using namespace std;
int isvowel(char c)
{
	switch(c)
	{
		case 'a':
		case 'A':
		case 'e':
		case 'E':
		case 'i':
		case 'I':
		case 'o':
		case 'O':
		case 'u':
		case 'U':
			return 1;
	}
	return 0;
}
int main()
{
	char s[1000];
	int n=0;
	while(cin.getline(s,1000))
	{
		int l=strlen(s);
		int pos=0;
		n++;
		while(s[pos]!=0)
		{
			char start=0;
			while(!((s[pos]>='a'&&s[pos]<='z')||(s[pos]>='A'&&s[pos]<='Z')||pos>=l))
			{
				cout<<s[pos];
				pos++;
			}
			if(s[pos]==0)break;
			if(!(isvowel(s[pos])))
			{
				start=s[pos];
				pos++;
			}
			while(((s[pos]>='a'&&s[pos]<='z')||(s[pos]>='A'&&s[pos]<='Z'))&&pos<l)
			{
				cout<<s[pos];
				pos++;
			}
			if(start)cout<<start;
			cout<<"ay";
		}
		cout<<"\n";
	}
	return 0;
}

gba356
New poster
Posts: 15
Joined: Sat Apr 28, 2007 10:12 am
Location: Taiwan

Re: Time Limit Exceeded in 492. Why?

Post by gba356 »

You should print '\n' after each line.

Also I'm not sure if a buffer with a size of 1000 would be enough..

Post Reply

Return to “Volume 4 (400-499)”