483 - Word Scramble

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

terry646623
New poster
Posts: 8
Joined: Fri Jan 17, 2014 3:35 pm

uva 483 Word Scramble

Post by terry646623 » Sat Feb 08, 2014 4:15 am

Why am I get output limit? Please help :cry:

Code: Select all

Got accepted when using while( get(letter) ) {...}
Thank You! :D
Last edited by terry646623 on Tue Mar 04, 2014 6:19 am, edited 2 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: uva 483 Word Scramble

Post by brianfry713 » Tue Feb 11, 2014 12:19 am

don't use system("pause");
EOF will never equal 1. You could instead do something like while(gets(letter)) {
Check input and AC output for thousands of problems on uDebug!

rajib2k10
New poster
Posts: 4
Joined: Fri Feb 10, 2012 12:02 am

UVA 483. Getting WA. Please Help.

Post by rajib2k10 » Fri May 16, 2014 3:03 pm

It is printing a space before start of each output.

Code: Select all

#include <algorithm>
#include <bitset>
#include <cstring>
#include <cctype>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <stdio.h>
#include <sstream>
#include <utility>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
#define LL long long

using namespace std;

int main()
{
    string input;
    char names[100];
    while(getline(cin,input))
    {
        istringstream iss(input);
        while(iss>>names)
        {
            int len = strlen(names);
            for(int i=len;i>=0;i--)
            {
                if(names[i] == ' ')
                {
                    continue;
                }
                cout<<names[i];
            }
        }
        cout<<"\n";
    }
return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: UVA 483. Getting WA. Please Help.

Post by brianfry713 » Fri May 16, 2014 9:50 pm

You're printing a null char at the end of each line.
Check input and AC output for thousands of problems on uDebug!

rajib2k10
New poster
Posts: 4
Joined: Fri Feb 10, 2012 12:02 am

Re: UVA 483. Getting WA. Please Help.

Post by rajib2k10 » Fri May 16, 2014 11:01 pm

Actually, I didn't get it. I didn't use a cout to print a null character. so please can you tell me where is the bug. if you can then please tell me what should i do to not print a null character so that i can learn that error. Thanks

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: UVA 483. Getting WA. Please Help.

Post by brianfry713 » Tue May 20, 2014 2:52 am

There is a null char at the end of each string. strlen returns the index to that null char and that is the first thing you're printing.
Check input and AC output for thousands of problems on uDebug!

Shadow_Coder
New poster
Posts: 4
Joined: Mon Jun 16, 2014 2:19 pm

Getting WA with 483 Word Scramble

Post by Shadow_Coder » Mon Jun 16, 2014 2:31 pm

Hello. I'm trying to solve the 483 Word Scramble problem, but I'm getting WA. When I run the code on my computer with the given test data, the result comes alright. But, it still gets a WA. Below given is my code. Can anybody help me?

Code: Select all

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

string strrev(string s){
    string t(s.rbegin(), s.rend());
    return t;
}

int main() {
	string s, t;
	while(!cin.eof()){
		getline(cin, s);
		istringstream buffer(s);
		while(buffer >> t){
			cout << strrev(t) << " ";
		}
		cout << "\n";
	}
	return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Getting WA with 483 Word Scramble

Post by brianfry713 » Mon Jun 16, 2014 8:44 pm

Don't print a space at the end of a line.
Check input and AC output for thousands of problems on uDebug!

Shadow_Coder
New poster
Posts: 4
Joined: Mon Jun 16, 2014 2:19 pm

Re: Getting WA with 483 Word Scramble

Post by Shadow_Coder » Tue Jun 17, 2014 9:33 am

Thanks, got AC! I had to prevent the newline and the space at the end of the last line. Below is the code.

Code: Select all

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

string strrev(string s){
    string t(s.rbegin(), s.rend());
    return t;
}

int main() {
   string s, t;
   while(!cin.eof()){
      getline(cin, s);
      istringstream buffer(s);
      while(buffer >> t){
         cout << strrev(t);
         if(!buffer.eof()) { cout << " "; }
      }
      if(!cin.eof()) cout << "\n";
   }
   return 0;
}

shaficse
New poster
Posts: 5
Joined: Sun Aug 17, 2014 8:20 pm

Re: Getting WA with 483 Word Scramble

Post by shaficse » Sun Aug 17, 2014 8:25 pm

I am getting presentation error .
Please someone help me?

#include <iostream>
#include <cstdio>
#include <cstring>


using namespace std;
void reverse_string(char* pch)
{
int len=strlen(pch);
char temp;
for(int i=0;i<len/2;i++)
{
temp=pch;
pch=pch[len-i-1];
pch[len-i-1]=temp;
}
printf("%s ",pch);
}


int main()
{

char str[10000];
while(gets(str))
{
char *pch;
pch=strtok(str," ");
while(pch!=NULL)
{
reverse_string(pch);
pch=strtok(NULL," ");
}
printf("\n");
}

return 0;
}

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: Getting WA with 483 Word Scramble

Post by lighted » Mon Aug 18, 2014 1:25 am

You are printing extra space at the end of line. You can make following changes

Code: Select all

printf("%s ",pch);
It must be

Code: Select all

printf("%s",pch);
Add one line

Code: Select all

while(pch!=NULL)
{
    reverse_string(pch);
    pch=strtok(NULL," ");
    if (pch != NULL) putchar(' ');
}
Don't forget to remove your code after getting accepted. 8)

By the way,

Code: Select all

use code tags
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

shaficse
New poster
Posts: 5
Joined: Sun Aug 17, 2014 8:20 pm

Re: 483 - Word Scramble

Post by shaficse » Sun Aug 24, 2014 10:52 pm

thank you lighted . i can not do this type of error again.

moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

Re: 483 - Word Scramble Runtime Error

Post by moxlotus » Sat Dec 06, 2014 6:28 pm

The following code executes perfectly fine on my machine but gets RE on UVa.

Code: Select all

 AC
Can someone tell me where the source of RE is in this piece of code? perhaps a test case that can cause RE will be much appreciated. :)
Last edited by moxlotus on Mon Dec 08, 2014 11:31 am, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 483 - Word Scramble

Post by lighted » Sun Dec 07, 2014 3:39 pm

Increase value of size by 1 for '\0' character.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

Re: 483 - Word Scramble

Post by moxlotus » Mon Dec 08, 2014 10:34 am

lighted wrote:Increase value of size by 1 for '\0' character.
Thanks.

Post Reply

Return to “Volume 4 (400-499)”