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

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

TLE Again

Post by vijay03 »

Hi Jan,

Thnx for replying. I implemented the changes u said in your post and my time wierdly increased from 10.076 to 10.111! How can this be possible?
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

AFter 10 seconds(limit) the judge automatically terminates the program. So, 10.076 to 10.111 is nothing(or you can say its for termination process).

Can you post your code? Remove the previous one.
Ami ekhono shopno dekhi...
HomePage
vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Post by vijay03 »

The code - the only thing i`ve changed is the vstrcat function

Code: Select all


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

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


void vstrcat(char p[],char q,int l)
{
	int x=l;
	p[x+1]=p[x];
	p[x]=q;
	
}

void vput(char a[],char b[],int x,int y)
{
	int i,l=strlen(a);
	for(i=x;i<=y;i++,l++)
		vstrcat(a,b[i],l);
}


void process(char a[],char b[])
{
	int i,s,e,state=0,bl,l=strlen(a);

	for(i=0;i<l;i++)
	{
		if(isalpha(a[i]))
		{
			if(state==0)
			{
				state=1;
				s=i;
			}
		}	
		else
		{
			if(state==1)
			{
				e=i-1;
				if(isvowel(a[s]))
				{
					vput(b,a,s,e);
					strcat(b,"ay");
				}
				else
				{
					vput(b,a,s+1,e);
					bl=strlen(b);
					vstrcat(b,a[s],bl);
					strcat(b,"ay");
					
				}				
			}
			state=0;
			bl=strlen(b);
			vstrcat(b,a[i],bl);
		}

		if((i==(l-1))&&(state))
		{
			e=l-1;
			if(isvowel(a[s]))
			{
				vput(b,a,s,e);
				strcat(b,"ay");
			}
			else
				{
					vput(b,a,s+1,e);
					bl=strlen(b);
					vstrcat(b,a[s],bl);
					strcat(b,"ay");
				}
		}


	}
}


int main()
{
	char a[2000000],b[2000000];
	while(gets(a))
	{
		if(!strlen(a))
			break;
		b[0]='\0';
		process(a,b);
		printf("%s\n",b);
	}
	return 0;
}

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

Post by Jan »

There are still..

Code: Select all

l=strlen(a); 
...
bl=strlen(b); 
Remove these. The reason is simple. Suppose b[] has 100000 characters, then strlen(b) will take a lot of time. After that if you add one character to b again, your code will find strlen(b) again. Thats why you are getting TLE.
Ami ekhono shopno dekhi...
HomePage
vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Post by vijay03 »

I used strlen since the string b will be dynamically changing and it will be hard to keep track of the size of the b. I suppose i`ll have to maintain its size in a global variable.

Is that how u solved the problem? Or did u use a different method entirely?
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Spoiler ahead...
I m describing my algorithm..

Code: Select all

1. Read the line ( like gets(a) )
2. i = j = 0
3. if a[i] is a letter
4.     b[j] = a[i]
5.     j++
6.     i++
7.     goto 3
8. else // a[i] is not a letter
9.     if j>0
10.        process b and print
11.        j = 0
12.    if a[i] = NULL then print a new line and goto 1
13.    print a[i]
14.    i++
15.    goto 3
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Thanks

Post by vijay03 »

Thanks for the algo.. Saw the same logic in some other post here.. Too lazy to change my program to that :P.. Was trying to somehow make my program fit into the timeframe! Guess i`ll have to make big changes now.. Thanks for ur help
ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

why WA?(492)piglatin

Post by ishtiaq ahmed »

Code: Select all

The code is removed after ac.
Last edited by ishtiaq ahmed on Sun Feb 17, 2008 7:32 pm, edited 1 time in total.
helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

There are many threads on this problem..
Try to search first.. and don't open a new thread if there is one already..
If you need to post, use one of the old one..
Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

Dear istiaq you have opened a lot of new threads
you shouldn't do that

and please send your code using code tag
Akash chhoyar swopno
Dream to touch the sky
bishop
New poster
Posts: 43
Joined: Fri May 04, 2007 12:57 pm

492 why WA

Post by bishop »

i search
but i can not found any solution

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define isalphabet(c) ((c>='a' && c<='z') || (c>='A' && c<='Z'))
#define vowel(n) (n=='a' || n=='e' || n=='i' || n=='o' || n=='u' || n=='A' || n=='E' || n=='I' || n=='O' || n=='U')
int main()
{
	char str[2000000];
	int l,i,j,k,m;	  
	
	while(gets(str))
	{
		
		l=strlen(str);
		
		i=0; k=0; j=0;
		
		while(isalphabet(str[0]) && i<l) 
		{
			if(vowel(str[i]))
			{
				for(j=i;isalphabet(str[j]) ; j++)
				{
					printf("%c",str[j]);
				}
				printf("ay%c",str[j]);
				i=j+1;
			}
			else if(!(vowel(str[i]))) 
				{
					m=i;
					for(k=i+1; isalphabet(str[k]); k++)
					{
						printf("%c",str[k]);
					}
					printf("%cay%c",str[m],str[k]);
					i=k+1;
				}
			else 
				printf("%c",str[i]);
				
				
			
		}
		
	
	}
	
	return 0;
}
:-?
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

You've been warned many times not to create a new thread for a problem if there are existing threads in which to post in.

If you keep doing this, your post will be deleted without further warning.
The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.
stanleymao
New poster
Posts: 1
Joined: Tue Jun 19, 2007 5:51 am

492 WA

Post by stanleymao »

Could somebody give me some test data
or tell me the error in the code?
I cannot find. Thanks a lot.
#include<stdio.h>

int main()
{
char input[10000];
int i, j;
int temp;

while( fgets(input, 10000, stdin) )
{
i=0;
temp=0;

while(input!='\0')
{
if( ( input>=65 && input<=90 ) || ( input>=97 && input<=122) )
{
if( input[i-1]<65 || (input[i-1]>90 && input[i-1]<97) || input[i-1]>122 )
{
if(input==65 || input==69 || input==73 || input==79 || input==85
|| input[i]==97 || input[i]==101 || input[i]==105 || input[i]==111 || input[i]==117)
{
printf("%c", input[i]);
}
else
{
temp=input[i];
}
}
else
{
printf("%c", input[i]);
}

if( input[i+1]<65 || (input[i+1]>90 && input[i+1]<97) || input[i+1]>122 )
{
if(temp!=0)
{
printf("%c", temp);
temp=0;
}
printf("ay");
}
}

else
{
printf("%c", input[i]);
}
i++;
}
}
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Search the board first. And dont open new threads if one already exists.
Ami ekhono shopno dekhi...
HomePage
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

492 TLE,help needed

Post by Fuad Hassan EWU »

:oops:
i am getting TLE for this prob. Plz help with tips. how can I make it faster

Code: Select all

DELETED AFTER AC :-)
Last edited by Fuad Hassan EWU on Wed Aug 01, 2007 6:30 am, edited 1 time in total.
Eagle er moto daana meley urbo
Post Reply

Return to “Volume 4 (400-499)”