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

tanvirfromhell
New poster
Posts: 5
Joined: Sat Apr 16, 2011 7:42 pm

Why compilation Error?!!!!

Post by tanvirfromhell »

Why I got compilation error?!!!

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char a[1000];
    while(scanf("%s",&a)==1)
    {
        if(a[0]=='\n' || a[1]=='\n')
        printf("\n");
        else
        printf("%s ",strrev(a));
    }
    return 0;
}
mahfuzsust001
New poster
Posts: 1
Joined: Sat Dec 10, 2011 7:17 am

483 TLE

Post by mahfuzsust001 »

Where is my mistake, I cant find it.. MY code is here......
#include <stdio.h>
#include <string.h>

int main()
{
char str[90000];
int i,j,t,len;
while(gets(str))
{
t=0;
len = strlen(str);
for(i=0;i<=len;i++)
{
if(str==' ' || i==len)
{
for(j=i-1;j>=t;j--) printf("%c",str[j]);
printf("%c",str);
t=i+1;
}
}
printf("\n");
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 483 TLE

Post by brianfry713 »

You're printing a null character at the end of each line. Don't use gets().

Use the existing thread for a problem. Use the code tags.
Check input and AC output for thousands of problems on uDebug!
PromeNabid
New poster
Posts: 21
Joined: Mon Jun 18, 2012 12:52 am
Location: Dhaka, Bangladesh.
Contact:

Re: 483

Post by PromeNabid »

I have a question, what should happen if there is a space right before end of line ?? My code does not get the line feed for this. I did not used "gets".
At first got Wa. I read all the clarifications then Accepted. Some samples by my AC code.
Input:

Code: Select all

I love you.
 
You  love me.
The output will consist 

of the same lines and words as the input file. 
However, the letters within each word must be reversed. 
Output:

Code: Select all

I evol .uoy
uoY evol .em
ehT tuptuo lliw tsisnoc
fo eht emas senil dna sdrow sa eht tupni .elif
,revewoH eht srettel nihtiw hcae drow tsum eb .desrever 
sonjbond
New poster
Posts: 19
Joined: Wed Jul 04, 2012 10:30 pm

483 PE

Post by sonjbond »

um getting PE ,,,, I/O is okk ... but PE ... plzzz help me.. m

Code: Select all


    code removed after AC
Last edited by sonjbond on Fri Jul 06, 2012 1:15 pm, edited 1 time in total.
sonjbond
New poster
Posts: 19
Joined: Wed Jul 04, 2012 10:30 pm

Re: 483 TLE

Post by sonjbond »

yaaa,, get AC now ... :)
stride
New poster
Posts: 2
Joined: Thu Aug 16, 2012 10:31 am

Re: 483

Post by stride »

i presume all my outputs are right, am i printing something invisible to the naked eye here? coz i got WA with this code..

Code: Select all

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
    char string[9999];
    char out[9999];
    int j=0,k=0;
    int length;
	while(scanf("\n%[^\n]",&string)!=EOF)
	{
        length = strlen(string);
        for(int i=0;i<length;i++)
        {
            if(string[i]==' ')
            {
                j=i-1;
                while(string[j]!=' ' && j>=0)
                {
                    out[k] = string[j];
                    j--;k++;
                }
                out[k] = ' ';
                k++;
            }
            else if(i == length-1)
            {
                j = length - 1;
                while(string[j]!=' ')
                {
                    out[k] = string[j];
                    j--;k++;
                }
            }
        }
        cout<<out<<endl;
        k=0;
    }
	return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 483

Post by brianfry713 »

Try the I/O gba356 posted in this thread. Your code ignores spaces at the beginning of a line.
Check input and AC output for thousands of problems on uDebug!
gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

Re: 483

Post by gr81 »

getting WA...please have a look...

#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
using namespace std;
void reverse(char *str, int start, int end)
{

if( start < end )
{
char tmp = str[start];
str[start] = str[end];
str[end] = tmp;
reverse(str, ++start, --end);
}
}
void process(char *str)
{
int len = strlen(str);

int start, end;
start = end = 0;

bool in_word = false;
int wc = 0;
for(int i=0; i <= len; ++i)
{
if( isalpha(str))
{
if(!in_word)
{
in_word = true;
start = i;
}
}
else if( (str == ' ') || (str == '\n') || (str == '\0'))
{
if(in_word)
{
in_word = false;
end = i-1;
reverse(str, start, end);
wc++;
}
}
}
}
int main()
{
vector<string> vs;
char str[90000] = {0};
while( scanf("%[^\n]", &str) != EOF)
{
getchar();
string lstr(str);

vs.push_back(lstr);
process((char*)lstr.c_str());
lstr.clear();
memset(&str, 0, sizeof(str));
}

vector<string>::iterator it;
for(it=vs.begin(); it != vs.end(); ++it)
{
cout << *it << endl;
}

return 0;
}

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

Re: 483

Post by brianfry713 »

Try updating your code to not print a newline at the end of a line if EOF is reached.
Check input and AC output for thousands of problems on uDebug!
gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

Re: 483

Post by gr81 »

sorry I didn't get your doubt....
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 483

Post by brianfry713 »

For input "asdf\n" the output should be "fsda\n".
For input "asdf", the output should be "fdsa", your code prints "fdsa\n".
Check input and AC output for thousands of problems on uDebug!
varrlo
New poster
Posts: 2
Joined: Mon Jul 15, 2013 6:42 pm
Location: Bydgoszcz, Poland

Re: 483 Word Scramble PE!!!!!!!!!!!!!

Post by varrlo »

Well I got WA and I have no idea what is the problem. Is it because of new line in the end of output?

Code: Select all

AC
Last edited by varrlo on Tue Jul 16, 2013 12:37 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 483 Word Scramble PE!!!!!!!!!!!!!

Post by brianfry713 »

For input a.b output should be b.a
Check input and AC output for thousands of problems on uDebug!
varrlo
New poster
Posts: 2
Joined: Mon Jul 15, 2013 6:42 pm
Location: Bydgoszcz, Poland

Re: 483 Word Scramble PE!!!!!!!!!!!!!

Post by varrlo »

So shall i just delete

Code: Select all

else if(lane[i] == '.')
? will it work?
nvm worked got AC
Post Reply

Return to “Volume 4 (400-499)”