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

LMY
New poster
Posts: 1
Joined: Tue Jan 23, 2007 10:31 am

492 WA help plz

Post by LMY » Mon Dec 08, 2008 1:08 pm

Here is my code.

I try some input by the wa discuss topic before but still get wa

can someone help me plz !!

Code: Select all

#include <iostream>

using namespace std;

#define MAX 1000000

int main(){
	char s[MAX];
	char c;
	bool begin;
	bool vowel;
	bool consonant;

	//freopen("in", "r", stdin);

	while (cin.getline(s, MAX)){
		begin = true;
		//vowel = true;
		for (int i=0; ;i++){
			// alphabet
			if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <='z')){
				if (begin){
					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')){
						c = s[i];
						consonant = true;
						vowel = false;
					}
					else{
						cout << s[i];
						consonant = false;
						vowel = true;
					}
					begin = false;
				}
				else
					cout << s[i];
			}
			else {
				if (consonant) {
					cout << c << "ay";
					consonant = false;
					vowel = false;
				}
				else if (vowel){
					cout << "ay";
					consonant = false;
					vowel = false;
				}
				if (s[i]!='\0')
					cout << s[i];
				else
					break;
				begin = true;
			}
		}
		cout << endl;
	}
	return 0;
}
Here is my input

Code: Select all

alex
Alex

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.
and output

Code: Select all

alexay
Alexay

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 UET
Bay.
anay aay eyeay Anay Aay Eyeay eeBay EEBay hakaDay-1205 1tsay astlay.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

492 run time error help plz

Post by sazzadcsedu » Wed Dec 17, 2008 8:39 pm

i got run time error .
but why???
cant understand!!
here is my code.

Code: Select all

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


                           int  main()


						 { 
							  int i,alph,flag,pos,k,len;
							  char str[100000];

                          //freopen("input.txt","r",stdin);
                          //freopen("output.txt","w",stdout);
                         


				  while(gets(str))

					  { 
                                 alph=0;				

                                   for(i=0;str[i];i++)

					  {
                                        if(isalpha(str[i]))
						   {
							   
                              
							   if(alph==0)
							   {
								   pos=i;
                                       
							   }
							   alph=1;

						   }
                               

                            else   if(!isalpha(str[i]) && alph==1)

							{  

							  if((str[pos]=='a') || (str[pos]=='e') || (str[pos]=='i') || (str[pos]=='o') ||  (str[pos]=='u') || (str[pos]=='A') || (str[pos]=='I') || (str[pos]=='E') || (str[pos]=='O')  || (str[pos]=='U'))
							  
								  flag=1;
							  else 
								   flag=0;
							       

							   alph=0;
								   
						  
						  

						    

						  
							     	if(flag==1)
								   
									{ 

									      for(k=pos;k<i;k++)

										  { 
										     printf("%c",str[k]);

										  }
								   
								             printf("ay%c",str[i]);

									} 


								    else 

									{

                                           for(k=pos+1;k<i;k++)

										  { 
										     printf("%c",str[k]);

										  }
							 
								           printf("%cay%c",str[pos],str[i]);
								   
								   
									}


						}
							else  
								 
								printf("%c",str[i]);


						}

					


				  		   
								   
					  len=strlen(str)-1;	
						
						if(isalpha(str[len]))

						
						
						
						{
						
						
						             if((str[pos]=='a') || (str[pos]=='e') || (str[pos]=='i') || (str[pos]=='o') ||  (str[pos]=='u') || (str[pos]=='A') || (str[pos]=='I') || (str[pos]=='E') || (str[pos]=='O')  || (str[pos]=='U'))
							  
					
									 { 

									      for(k=pos;k<i;k++)

										  { 
										     printf("%c",str[k]);

										  }
								   
								             printf("ay");

									 } 

						     

									    else 

									{

                                           for(k=pos+1;k<i;k++)

										  { 
										     printf("%c",str[k]);

										  }
							 
								           printf("%cay",str[pos]);
								   
								   
									}

						
						
						
						}       
							 printf("\n");

						}

						  return 0;
					}
						
						
	
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 492 WA help plz

Post by Articuno » Wed Dec 17, 2008 9:21 pm

Make your array size 1000000.
Declare it as global.
And you should use long instead of int. Rest of the code is ok I think.
Good luck
May be tomorrow is a better day............ :)

newkid
Learning poster
Posts: 73
Joined: Fri Dec 12, 2008 3:06 am

Re: 492 WA help plz

Post by newkid » Wed Dec 17, 2008 9:26 pm

@sazzadcsedu

My guess is

Code: Select all

char str[100000];
there might be more than 100000 characters in one line..
Reading character by character is another alternative. Or you can try with increasing the limit..
hmm..

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 492 WA help plz

Post by sazzadcsedu » Wed Dec 17, 2008 10:33 pm

newkid says:
there might be more than 100000 characters in one line..
Reading character by character is another alternative. Or you can try with increasing the limit..
but i tried with str[500000 /1000000].
but the result is same .
run time error.
can u give me a input file???
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 492 WA help plz

Post by Articuno » Wed Dec 17, 2008 10:46 pm

@ sazzadcsedu,
Just change this portion of your code:

Code: Select all

removed
into this:

Code: Select all

removed
You will get AC. :)
Last edited by Articuno on Fri Dec 19, 2008 4:11 pm, edited 2 times in total.
May be tomorrow is a better day............ :)

newkid
Learning poster
Posts: 73
Joined: Fri Dec 12, 2008 3:06 am

Re: 492 WA help plz

Post by newkid » Wed Dec 17, 2008 11:03 pm

LMY wrote:Here is my code.

I try some input by the wa discuss topic before but still get wa

can someone help me plz !!

Code: Select all

#include <iostream>
#define MAX 1000000

int main(){
	char s[MAX];

	while (cin.getline(s, MAX)){
		begin = true;
		//vowel = true;
The input file has more than 10^6 characters per line..
you were getting WA instead of RTE because you used "cin.getline(s, MAX)" which is arraysafe..
you would have got RTE if you used gets(s)..
hmm..

newkid
Learning poster
Posts: 73
Joined: Fri Dec 12, 2008 3:06 am

Re: 492 WA help plz

Post by newkid » Wed Dec 17, 2008 11:07 pm

@sazzadcsedu

Code: Select all

#include <stdio.h>
#include <assert.h>
const long long MAX = 10000000;
int main() {
  char ch;
  long long cnt;
  while (scanf("%c", &ch) == 1) {
    cnt = 0;
    do {
      if (ch == EOF)   break;
      if (ch == '\n') {
        assert(cnt < MAX);
        break;
      }
      cnt++;
    } while (scanf("%c", &ch) == 1);
  }

  return 0;
}
with MAX = 1000000 this code gets RTE
with MAX = 10000000 this gets WA as expected
so update your arraysize to 10^7 or use scan character by character..
hmm..

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: at last accepted

Post by sazzadcsedu » Thu Dec 18, 2008 1:12 am

thanx to @articuno.

i did according to ur tips &&&& got accepted.

also thanx to @newkid for ur help.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 492 WA help plz

Post by Articuno » Thu Dec 18, 2008 1:21 am

@sazzadcsedu
Don't forget to remove your code.
Good luck
May be tomorrow is a better day............ :)

toru
New poster
Posts: 17
Joined: Tue Dec 30, 2008 10:38 am

492 RTE: PLZZZ HELP ......................

Post by toru » Sat Jan 10, 2009 9:49 pm

HI every1,

I m trying to accept this code for around 7 days but every time it gives RTE, i increased the array size but stil the same. i attached my code below plzzzz any1 HELP ME..........................

Code: Select all

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

int isvowel(char v_in)
{
	int flag = 0;

	if(v_in=='A' || v_in=='a')
		flag=1;
	else if(v_in=='E' || v_in=='e')
		flag=1;
	else if(v_in=='I' || v_in=='i')
		flag=1;
	else if(v_in=='O' || v_in=='o')
		flag=1;
	else if(v_in=='U' || v_in=='u')
		flag=1;
	
		return flag;
}

int main()
{
	char in[MAX], ch;
	long i=0, vowel=0, len=0;

	while(gets(in))
	{
		if(strcmp(in, " ")==0)
			continue;

		len=strlen(in);

		for(i=0; i<len; i++)
		{
    		if( isalpha(in[i]) )
			{
				ch=in[i++];
				vowel=isvowel(ch);
				
				if(vowel)
					printf("%c", ch);
				while(isalpha(in[i]) && i<len)
				{
					printf("%c", in[i]);
 					i++;
				}
				if(!vowel)
					printf("%c", ch);
				
				printf("ay");
				i--;
			}
			else
				printf("%c", in[i]);
		}
		printf("\n");
	}
	return 0;
}

:cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

BTMouseKing
New poster
Posts: 1
Joined: Wed Dec 31, 2008 8:26 am

492: WA, dont know why...

Post by BTMouseKing » Tue Jan 13, 2009 2:22 am

Hi all, I have read all the threads on 492 and tried every method and sample inputs/outputs given in previous posts. However, my code still still gets WA. I don't know wats wrong. I'm wondering about the definition of the 'word' (consecutive sequence of letters). Will 'a' be considered as a word? If the input is "This is a man." Will the output be "hisTay isay a anmay." or "hisTay isay aay man."?

Here is my code:

Code: Select all


#include "iostream"
#include "string.h"
using namespace std;

int type_character(char c)
{
	if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return 1;
	else return 0;
}
int is_vowel(char c)
{
	if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || 
		c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')	return 1;
	else return 0;
}
int main()
{
	string input;
	string output;
	char input_ch[1000000];
	char temp_ch[1000] = {};
	int temp_ch_index = 0;
	char temp[2] = {};
	char record;
	fflush(stdin);
	while (getline(cin,input))
	{
		output = "";
		input += ".";
		strcpy(input_ch,input.c_str());
		for (long i=0;i<input.length();i++)
		{
			if (type_character(input_ch[i]))
			{
				temp_ch[temp_ch_index] = input_ch[i];
				temp_ch_index++;
			}
			else
			{
				if(temp_ch[0] == '\0')	;
				else
				{
					if (is_vowel(temp_ch[0]))	;
					else
					{
						record = temp_ch[0];
						for (int j=0;j<temp_ch_index-1;j++)
							temp_ch[j] = temp_ch[j+1];
						temp_ch[temp_ch_index-1] = record;
					}
					output += temp_ch;
					output += "ay";
				}
				temp[0] = input_ch[i];
				output += temp;
				temp_ch_index = 0;
				memset(temp_ch,0,1000);
			}
		}
		output = output.substr(0,output.length()-1);
		cout << output << endl;
		memset(input_ch,0,1000000);
	}
	return 0;
}

User avatar
theharshest
New poster
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

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

Post by theharshest » Sun Jan 18, 2009 9:27 pm

I am getting WA for the following code.. Pease help.. :(

Code: Select all

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s,str;
	long long c,t,i;

	while(getline(cin,s))
	{
		c=0;
		while(c<s.length())
		{
			if(c==0)
				t=s.find(" ",c);
			else
				t=s.find(" ",c+1);
			
			//cout<<c<<" "<<t<<endl;
	
			
				str=s.substr(c,t-c+1);
			

			//cout<<str<<endl;

			if(str[0]=='a' || str[0]=='e' || str[0]=='i' || str[0]=='o' || str[0]=='u' || str[0]=='A' || str[0]=='E' || str[0]=='I' || str[0]=='O' || str[0]=='U' )
			{
				if((str[str.length()-1]>=65 && str[str.length()-1]<=90) || (str[str.length()-1]>=97 && str[str.length()-1]<=122))
				{
					cout<<str<<"ay";
				}
				else
				{
					for(i=0;i<str.length()-1;i++)
						cout<<str[i];
					cout<<"ay"<<str[str.length()-1];
				}
			}
			else
			{
				if((str[str.length()-1]>=65 && str[str.length()-1]<=90) || (str[str.length()-1]>=97 && str[str.length()-1]<=122))
				{
					for(i=1;i<str.length();i++)
						cout<<str[i];
					cout<<str[0]<<"ay";
				}
				else
				{
					for(i=1;i<str.length()-1;i++)
						cout<<str[i];
					cout<<str[0]<<"ay"<<str[str.length()-1];
				}
			}
			
			c+=str.length();
		}
		cout<<endl;
	}
}
"if u r goin thru hell, keep goin"

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

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

Post by mf » Mon Jan 19, 2009 12:44 am

Note this sentence:
A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).
So, words might not be separated by spaces. In fact there may be no single space in the input. For example, the following input has four words:

Code: Select all

This,is*the-input.

qwerty
New poster
Posts: 21
Joined: Sun Feb 08, 2009 5:26 pm
Location: Mumbai,India

Re: 492 - Pig Latin

Post by qwerty » Wed Feb 18, 2009 8:50 pm

ac..removed
Last edited by qwerty on Wed Oct 28, 2009 6:28 am, edited 1 time in total.

Post Reply

Return to “Volume 4 (400-499)”