492 - Pig-Latin
Moderator: Board moderators
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Change your my_p() function.
Hope it works.
Code: Select all
int my_p(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return 0;
else
return 1;
}
Ami ekhono shopno dekhi...
HomePage
HomePage
I don't know pascal. But I can say that this problem requires a big array. My array size was 1000000.
Hope it helps.
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 4
- Joined: Wed Nov 10, 2004 8:47 pm
492 Wrong Answer why ??
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??
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??
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
492-- Why I Get WA ??? Please Help...
#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;
}
#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;
}
492 WA!!WHO CAN GIVE ME SOME TEST DATA??!!THANKS A LOT
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;
}
Try this 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:
There are test cases in past threads. Just do a search for 492.
Code: Select all
This is the input.
This.is.the.input.
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:
It does not mean the words are separated by exactly 1 space.A "word" is defined as a consecutive sequence of letters (upper and/or lower case)
There are test cases in past threads. Just do a search for 492.
Explain a difference
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;
}
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;
}
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;
}
#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;
}
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
str.data() does not have to be terminated by 0. Use str.c_str() instead.Zaspire wrote:And get WA with this:Code: Select all
... printf("%s",str.data()); ...
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
I don't know. Possibly, VS2005 has a different implementation of strings than GCC. The STL documentation states:Zaspire wrote:Why on my computer (Visual Studio 2005) this code work?
Have you already tried to submit that code with str.data() exchanged by str.c_str()? Has it helped?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.
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
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/.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.