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

Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Re: 483 WA

Post by Piotrek Mazur »

oulongbin wrote:char *a;
a=new char;
I think this case error: size of a is 1 char only.

A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

As i know every word is less then 100 char long
so take an array of char, read a word
and just print every char from (len-1 to 0)
no need to cheak any thing else.

It is a very easy problem :wink:

oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

Post by oulongbin »

Thanks.I have changed my code.Now got AC.
Thank you very much! :D

coolyaya
New poster
Posts: 1
Joined: Sat Oct 16, 2004 7:58 am

483 why WA help plz

Post by coolyaya »

This is my code...I can't find the bug...please help!

[c]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int length,s,i,j,k,p,q=0;
char a[1000];

int main()
{
while(gets(a))
{
if(q>=1)
printf("\n");
string(a);
q++;
}

return 0;
}

int string(char a[])
{
s=0;
k=0;
length=strlen(a);
for(i=0;i<length;i++)
if(a==32)
s++;

for(i=0;i<length;i++)
{
if(a!=32)
k++;

if(a==32)
{
for(j=i-1;j>=i-1-k;j--)
printf("%c",a[j]);
k=0;
s--;
}
if(s==0)
{
for(j=length-1;j>=i+1;j--)
printf("%c",a[j]);
break;
}
}
}[/c]

Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega »

Hi !! coolyaya this some I/O
input :

Code: Select all

I love you.											roma
You love me
We're a happy family.
jajaja ummm             adfdsad sdfdasfnfoqf        pnfoqifowen    nowiqorowe
This is the input. why WA NOOOOOOOOO!!!!!!
SIHT SI 			TUPNI	GRRRR		TWO
LOOL 													faf			sa
ouput :

Code: Select all

I evol .uoy											amor
uoY evol em
er'eW a yppah .ylimaf
ajajaj mmmu             dasdfda fqofnfsadfds        newofiqofnp    eworoqiwon
sihT si eht .tupni yhw AW !!!!!!OOOOOOOOON
THIS IS 			INPUT	RRRRG		OWT
LOOL 													faf			
Hope it help
Keep posting !! :D

kuonet
New poster
Posts: 1
Joined: Sat Oct 23, 2004 6:28 pm

Post by kuonet »

I also get a WA but I don't know where the mistake happened.
Here is the code, Thanks!
[c]
#include<stdio.h>
#include<string.h>

int main()
{
int n,count,j,tmp,i,k,max;
char a[10000] ="";
int b[10000] ={0};
while(gets(a) != NULL)
{
n = strlen(a);
count = 0;
j=0;
tmp = 0;
for(i=0;i<=n-1;i++)
{
if(a==' '){count++;b[j++]=i;}
}
if(count==0)
{
if(n>2)
for(i=0;i<=n/2;i++)
{
tmp=a;
a=a[n-i-1];
a[n-i-1]=tmp;
}
else if(n==2)
{
tmp=a[1];
a[1]=a[0];
a[0]=tmp;
}
}
else if(count==1)
{
k=b[0]-1;
for(i=0;i<=(b[0]-1)/2;i++)
{
tmp=a;
a=a[k];
a[k]=tmp;
k--;
}
k=n-1;
for(i=b[0]+1;i<=(b[0]+n)/2;i++)
{
tmp=a;
a=a[k];
a[k]=tmp;
k--;
}
}
else if(count!=0&&count!=1)
{
for(i=0;b[i+1]!=0;i++)
{
k = b[i+1]-1;
for(j=b+1;j<=(b+b[i+1])/2;j++)
{
tmp=a[j];
a[j]=a[k];
a[k]=tmp;
k--;
}
}
k=b[0]-1;
if(k<0){}
else if(k>=0)
{
for(i=0;i<=(b[0]-1)/2;i++)
{
tmp=a;
a[i]=a[k];
a[k]=tmp;
k--;
}
}
for(i=0;i<=n-1;i++)
{
if(b[i]==0&&i!=0){max=i-1;break;}
}
k=n-1;
for(i=b[max]+1;i<=(n+b[max])/2;i++)
{
tmp=a[i];
a[i]=a[k];
a[k]=tmp;
k--;
}
}
for(i=0;i<=n-1;i++)
{
printf("%c",a[i]);
}
printf("\n");
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
}
return 0;
}
[/c]

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal »

Dear kuonet, I found some mistake. Here are some inputs and the correct outputs. Your program fails in these cases.

INPUT

..aa
..as
aa..
as..

OUTPUT ( should be )

aa..
sa..
..aa
..as

YOUR PROGRAM SAYS

a.a.
s.a.
.a.a
.s.a

p!ter
New poster
Posts: 11
Joined: Thu Nov 18, 2004 8:55 pm

483 strange error!!!

Post by p!ter »

Hi!

My input:

I love you.
You love me.
We're a happy family.

My output:

.uoy evol I
.em evol uoY
.ylimaf yppah a er'eW

I dont know what is wrong!!

Here is my code:

#include<stdio.h>

int max=2000000;

int main()
{
char temp,t[max];
int i,j,p,q;

while(fgets(t, max, stdin))
{
for(i=0; i<max; i++)
{
if(t=='\n')break;
if(t!='\32')
{
j=i+1;
while(j<max)
{
if(t[j]=='\n')break;
if(t[j]!='\32')j++;
else break;
}
j=j-1;
q=j;
for(p=i; p<=q; p++)
{
temp=t[p];
t[p]=t[q];
t[q]=temp;
q--;
}
i=j;
}
}
for(i=0; i<max; i++)
{
if(t!='\0')
{
printf("%c",t);
}
else break;
}
}
return 0;
}

Please help me!!
Thx.

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Post by Heartattack! »

You're supposed keep the order of words correct:

I love you.=
I evol .uoy

See the sample io. Good luck.
We will, We will BREAK LOOP!!!!

Karthekeyan
New poster
Posts: 33
Joined: Tue Jun 29, 2004 1:38 pm
Location: IITM,chennai,Tamil Nadu,India
Contact:

Strangely, I am getting TLE for 483

Post by Karthekeyan »

Here is the simplest code I think is possible for problem 483, and i get TLE for this code....is there any better algo than this?

Code: Select all

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
main()
{
  char ch;
  while(ch=getchar())
    {
      vector<char> st;
      while(ch!='\n')
	{
	  if(ch!=' ')
	    {
	      st.push_back(ch);
	    }
	  else
	    {
	      for(int j=st.size()-1;j>=0;j--)
		{
		  cout<<st[j];
		}cout<<" ";
	      st.clear();
	    }
	  ch=getchar();
	}
      for(int j=st.size()-1;j>=0;j--)
	{
	  cout<<st[j];
	}
      cout<<'\n';
    }
}
Karthe

mohiul alam prince
Experienced poster
Posts: 120
Joined: Sat Nov 01, 2003 6:16 am
Location: Dhaka (EWU)

Post by mohiul alam prince »

hi
please check why u have got AC with PE
i have changed ur code

Code: Select all

#include<vector> 
#include<cstdio> 
#include <string.h>

using namespace std; 
main() 
{ 
  char ch; 
  while(ch=getchar()) 
  { 
	  if ( ch == EOF)break;
      vector<char> st; 
      while(ch!='\n') 
	  { 
			if(ch!=' ') 
			{ 
				st.push_back(ch); 
			} 
			else 
			{ 
				for(int j=st.size()-1;j>=0;j--) 
				{ 
					cout<<st[j]; 
				}
				cout<<" "; 
				st.clear(); 
			} 
			ch=getchar();
			if ( ch == EOF)break;
	  } 
      for(int j=st.size()-1;j>=0;j--) 
	  { 
		    cout<<st[j]; 
	  } 
      cout<<'\n'; 
    } 
	return 0;
} 
MAP[/code]

Karthekeyan
New poster
Posts: 33
Joined: Tue Jun 29, 2004 1:38 pm
Location: IITM,chennai,Tamil Nadu,India
Contact:

Post by Karthekeyan »

thanx for that....i will c the P.E....
Karthe

thinker
New poster
Posts: 4
Joined: Mon Mar 28, 2005 8:38 pm

483 accepted but PE !!!! PLZ HELP ME

Post by thinker »

My 483 is accepted but i got PE . i dont understant why i got it please help me. my code is following.
#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

string str1;
string str2=" ";
char s[20000];


int main()
{
//freopen("input.in","r",stdin);

while(gets(s)!=NULL)
{

str1=s;
string str=str1+str2;
int l=0,pos=str.find(" ");
int size=str.find_last_of(str);



for(int i=0;i<=size;i++)
{
if(str.at(i)==' ')
{

if(pos!=size)
{
for(int i=pos-1;i>=l;i--)
{
cout<<str;
}

cout<<" ";
l=pos+1;
pos=str.find(" ",pos+1);
}


else
{
for(int i=pos-1;i>=l;i--)
{
cout<<str;
}
}


}


}
printf("\n");
}
return 0;
}

Zyaad Jaunnoo
Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

Post by Zyaad Jaunnoo »

A PE is good enough :wink:

sunnycare
Learning poster
Posts: 74
Joined: Tue Mar 08, 2005 2:35 am
Location: China , Shanghai

483 WA i have searched all samples,and all are right....

Post by sunnycare »

I have searched all sample inputs on board, and all are right...

Why judge still give me a WA instead of a AC :(

here is my code:

Code: Select all

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

string word;

char ch;

void reverseOut(string &s)
{
	long i=s.length()-1;
	while(i>=0)
	{
		cout<<s[i];
		i--;
	}
}
void main()
{
	
	while(cin.get(ch))
	{
		if(ch==' '||ch=='\n')
		{
			reverseOut(word);
			cout<<ch;
			word="";
		}
		else
		{
			word+=ch;
		}
	}
}

Waiting your help!!![/code]

Post Reply

Return to “Volume 4 (400-499)”