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

Post Reply
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

now remove this code immidiately
never send your ACC code here

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

Post by Jan »

Change your my_p() function.

Code: Select all

int my_p(char ch) 
{ 
    if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) 
        return 0; 
    else 
        return 1; 
} 
Hope it works.
Ami ekhono shopno dekhi...
HomePage

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

Post by Jan »

I don't know pascal. But I can say that this problem requires a big array. My array size was 1000000.

Hope it helps.
Ami ekhono shopno dekhi...
HomePage

late_nighter
New poster
Posts: 4
Joined: Wed Nov 10, 2004 8:47 pm

492 Wrong Answer why ??

Post by late_nighter »

Iam getting wrong answer for the 492 question. I have checked most of the
cases I can think of .

<code>

#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main ()
{
string x = "";
char ch;
while ((ch = getchar ()) != EOF)
{
if (isalpha (ch))
{
x+= ch;
}
else{
if ( toupper (x[0]) == 'A' ||
toupper (x[0]) == 'E' ||
toupper (x[0]) == 'I' ||
toupper (x[0]) == 'O' ||
toupper (x[0]) == 'U'
)
{
x+= "ay";
}else
{
if (x!="")
{
char c = x[0];
x = x.substr(1,x.size()-1);
x+=c;
x+="ay";
}
}
cout << x ;
cout << ch ;
x = "";
}
}

if (x!= "")
{
if ( toupper (x[0]) == 'A' ||
toupper (x[0]) == 'E' ||
toupper (x[0]) == 'I' ||
toupper (x[0]) == 'O' ||
toupper (x[0]) == 'U'
)
{
x+= "ay";
}else
{
if (x!="")
{
char c = x[0];
x = x.substr(1,x.size()-1);
x+=c;
x+="ay";
}
}
cout << x ;
cout << ch ;
x = "";

}
}

</code>

Can anyone tell me for which case is the program failing??

Solaris
Learning poster
Posts: 99
Joined: Sun Apr 06, 2003 5:53 am
Location: Dhaka, Bangladesh
Contact:

Post by Solaris »

before checking

toupper(x[0]) == SOMETHING

u should check whether x == "" or not
Where's the "Any" key?

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

492-- Why I Get WA ??? Please Help...

Post by jan_holmes »

#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <cstring>

// Wrong Answer ... :(

using namespace std;

int main () {
char n[2000000];
string x;
string ret;
while(cin.getline(n,2000000)) {
ret = "";
x = "";
for (int i=0;n;i++) {
if (isalpha(n)) x+=n;
else {
if (x == "") ret+=n;
else {
if (x[0] == 'A' || x[0] == 'E' || x[0] == 'I' || x[0] == 'O' || x[0] == 'U') {
for (int j=0;x[j];j++) {
ret+=x[j];
}
ret+="ay";
ret+=n;
x = "";
}
else if (x[0] == 'a' || x[0] == 'e' || x[0] == 'i' || x[0] == 'o' || x[0] == 'u') {
for (int j=0;x[j];j++) {
ret+=x[j];
}
ret+="ay";
ret+=n;
x = "";
}
else {
for (int j=1;x[j];j++) {
ret+=x[j];
}
ret+=x[0];
ret+="ay";
ret+=n;
x = "";
}
}
}
}
if (x != "") {
if (x[0] == 'A' || x[0] == 'E' || x[0] == 'I' || x[0] == 'O' || x[0] == 'U') {
for (int j=0;x[j];j++) {
ret+=x[j];
}

ret+="ay";
x = "";
}
else if (x[0] == 'a' || x[0] == 'e' || x[0] == 'i' || x[0] == 'o' || x[0] == 'u') {
for (int j=0;x[j];j++) {
ret+=x[j];
}

ret+="ay";
x = "";
}
else {
for (int j=1;x[j];j++) {
ret+=x[j];
}
ret+=x[0];
ret+="ay";
x = "";
}
}

cout << ret << "\n";
}
return 0;
}

Staryin
New poster
Posts: 12
Joined: Fri Dec 16, 2005 4:22 pm
Location: shanghai/china
Contact:

492 WA!!WHO CAN GIVE ME SOME TEST DATA??!!THANKS A LOT

Post by Staryin »

Code: Select all

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctype.h>
using namespace std;
char c[10000000];
char *word[10000];
int i,j,k,cnt,len;
char *p;
char punc;
void solve()
{
	 punc='\0';
	 len=strlen(word[cnt-1]);
	 for(i=0;i<len;i++)
	 	if(!isalpha(word[cnt-1][i]))
	 		{
	 			punc=word[cnt-1][i];
				len=len-1;
			}
	
	 if(word[cnt-1][0]=='a'||word[cnt-1][0]=='A'||word[cnt-1][0]=='e'
				 	||word[cnt-1][0]=='E'||word[cnt-1][0]=='i'||word[cnt-1][0]=='I'
				 	||word[cnt-1][0]=='O'||word[cnt-1][0]=='o'||word[cnt-1][0]=='u'
				 	||word[cnt-1][0]=='U')
				{
					
					for(k=0;k<len;k++)
						cout<<word[cnt-1][k];	
					cout<<"ay";
					cout<<punc;
					cout<<" ";
				}		
				else
				{
					
					for(k=1;k<len;k++)
						cout<<word[cnt-1][k];
					cout<<word[cnt-1][0]<<"ay";
					cout<<punc;
					cout<<" ";
				
				}
	

}
void deal()
{
	cnt=0;
	p = strtok(c," "); 
	if (p)
		{
			word[cnt++]=p;  
			solve();
			
		}
	while(p)
	{
		p = strtok(NULL," ");  

	 if (p)
	 	 {
	 	 	word[cnt++]=p;
	 	 	solve(); 
	 	} 
	}
	cout<<endl;
}
void read()
{
		while(gets(c))
			deal();
}
int main()
{
	read();
	return 0;	
}

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Try this input:

Code: Select all

This  is  the  input.
This.is.the.input.
2 spaces separate each word for the first line.
And for the second line, the period '.' separates each word.

Your code will fail for these test cases because you used strtok. You are assuming only 1 space separate each word.

The problem description says:
A "word" is defined as a consecutive sequence of letters (upper and/or lower case)
It does not mean the words are separated by exactly 1 space.

There are test cases in past threads. Just do a search for 492.

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Explain a difference

Post by Zaspire »

Explain a difference
A get AC with this code(492)
#include <stdio.h>
#include <ctype.h>

int alpha(int c)
{
if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')) return 1;
return 0;
}

int main()
{
int c,last,t;
c=getchar();
while (c!=EOF)
{
last=tolower(c);
if (alpha(c)) t=1;
else t=0;
if ((t==1)&&(!((last=='a')||(last=='e')||(last=='i')||(last=='o')||(last=='u')))) {last=c; c=getchar();}
else last=0;
while (alpha(c))
{
putchar(c);
c=getchar();
}
if (last!=0) putchar(last);
if (t==1) {putchar('a');putchar('y');}
if (c!=EOF) {putchar(c); c=getchar();}
}
return 0;
}

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Post by Zaspire »

And get WA with this:
#include <stdio.h>
#include <string>
#include <ctype.h>

int alpha(int c)
{
if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')) return 1;
return 0;
}

int main()
{
int c,last;
std::string str;
c=getchar();
while (c!=EOF)
{
str="";
last=tolower(c);
if (alpha(c)&&(!((last=='a')||(last=='e')||(last=='i')||(last=='o')||(last=='u')))) {last=c; c=getchar();}
else last=0;
while (alpha(c))
{
str+=(char)c;
c=getchar();
}
if (last!=0) str+=(char)last;
if (str!="") str+="ay";
printf("%s",str.data());
if (c!=EOF) {putchar(c); c=getchar();}
}
return 0;
}

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko »

Zaspire wrote:And get WA with this:

Code: Select all

		...
		printf("%s",str.data());
		...
str.data() does not have to be terminated by 0. Use str.c_str() instead.

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Post by Zaspire »

Why on my computer (Visual Studio 2005) this code work?

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko »

Zaspire wrote:Why on my computer (Visual Studio 2005) this code work?
I don't know. Possibly, VS2005 has a different implementation of strings than GCC. The STL documentation states:
STL documentation wrote:const charT* data() const: Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents. data() is permitted, but not required, to be identical to c_str(). The first size() characters of that array are guaranteed to be identical to the characters in *this. The return value of data() is never a null pointer, even if size() is zero.
Have you already tried to submit that code with str.data() exchanged by str.c_str()? Has it helped?

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Post by Zaspire »

Thanks for the help.
It has really helped.

Anybody known other different between VS 2005 and GCC?
I like VS 2005. But I have many error because In VS has a different implementation than GCC.

__________
Sorry for my bad English.

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko »

Zaspire wrote:Anybody known other different between VS 2005 and GCC?
I like VS 2005. But I have many error because In VS has a different implementation than GCC.
I don't know, I've never used VS. It may be helpful for you to compile your solutions also by GCC before submiting. There is a version of GCC for MS windows, too. Look at http://www.mingw.org/.

Post Reply

Return to “Volume 4 (400-499)”