490 - Rotating Sentences

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

aliahmed
New poster
Posts: 24
Joined: Fri Oct 24, 2008 8:37 pm
Location: CUET, Chittagong, Bangladesh
Contact:

Re: 490-Rotating sentence

Post by aliahmed » Fri Nov 14, 2008 10:28 pm

Thanks kabir bhai.
Last edited by aliahmed on Wed Apr 22, 2009 10:47 pm, edited 1 time in total.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 490-why WA

Post by abid_iut » Sat Nov 22, 2008 8:07 pm

it is giving right output for possible inputs but why WA
here is my code:

Code: Select all

#include<stdio.h>
#include<string.h>

#define MAX 500

char str1[MAX][500];

int main()
{
	char line[100001];
	long j=0,i,k,temp,len;
	while(gets(line)){
		len=strlen(line);
		if(len>temp)temp=len;
		strcpy(str1[j++],line);
	}
	for(i=0;i<temp;i++){
		for(k=j-1;k>=0;k--){
			printf("%c",str1[k][i]);
		}
		printf("\n");
	}
	return 0;
}
please help somebody :cry:
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

gba356
New poster
Posts: 15
Joined: Sat Apr 28, 2007 10:12 am
Location: Taiwan

Re: 490-why WA

Post by gba356 » Sun Nov 23, 2008 3:12 am

Hi, your solution don't even pass the sample test cases.

In the sample output we have:

Code: Select all

"R
Ie
*n
te
h*
iD
ne
kc
,a
*r
tt
he
es
r
eo
fn
oc
re
e
*s
Ia
*i
ad
m,
.
"
where I replaced the blanks with asterisks.

But your code gives:

Code: Select all

"R
Ie
*n
te
h*
iD
ne
kc
,a
*r
tt
he
es
r*
eo
fn
oc
re
e*
*s
Ia
*i
ad
m,
.*
"*
I think that is the problem:)

User avatar
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 490-why WA

Post by kbr_iut » Sun Nov 23, 2008 3:53 am

abid_iut wrote:it is giving right output for possible inputs but why WA
here is my code:

Code: Select all


	long j=0,i,k,temp,len;
	while(gets(line)){
		len=strlen(line);
		if(len>temp)temp=len;
		strcpy(str1[j++],line);
	}
I wonder how this code got WA. it should get RE coz...the variable tem.
tem need to be initialized before comparing with len....
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 490-why WA

Post by mf » Mon Nov 24, 2008 8:29 am

kbr_iut wrote:I wonder how this code got WA. it should get RE coz...the variable tem.
tem need to be initialized before comparing with len....
Actually, it's quite likely that temp would be initialized to zero: he has declared a rather large array just before it, so most of the changes on program's stack before main() is called would happen only in the beginning of that array, and temp would be the value used to fill memory allocated for stack, i.e. zero.

The problem, as gba356 already noticed, is that the code prints extra blanks sometimes. And those are not even spaces, but \0's.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 490-Rotating sentence now PE makes me crazy

Post by abid_iut » Tue Nov 25, 2008 9:37 am

Before it was WA i changed my code and do what the previous posts told me to do
but now it becomes PE
please help
my code again:

Code: Select all

#include<stdio.h>
#include<string.h>

#define MAX 500

char str1[MAX][500];

int main()
{
	char line[100001];
	long j=0,i,k,temp=0,len;
	while(gets(line)){
		len=strlen(line);
		if(len>temp)temp=len;
		strcpy(str1[j++],line);
	}
	for(i=0;i<temp;i++){
		for(k=j-1;k>=0;k--){
			if(str1[k][i]!=NULL){
				if(k==j-1 && str1[k][i]==' '){continue;}
				else 
				printf("%c",str1[k][i]);
			}
		}
		printf("\n");
	}
	return 0;
}
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

gba356
New poster
Posts: 15
Joined: Sat Apr 28, 2007 10:12 am
Location: Taiwan

Re: 490-Rotating sentence

Post by gba356 » Thu Nov 27, 2008 4:18 am

Still your code generates incorrect output for the sample input.

Why did you ignore the spaces in the first column?

All you have to ignore are the spaces redundant at the end of the line.

This line may help you:

Code: Select all

freopen("out.txt","w",stdout);
Keep working:)

fR0D
New poster
Posts: 29
Joined: Mon Feb 11, 2008 5:59 am
Contact:

Re: 490 : why WA with this simple looking problem?

Post by fR0D » Wed Dec 10, 2008 3:46 pm

Please tell me why m i getting PE?

Code: Select all

Got AC
sorry to say but there are more confusing posts on this than clarifying.
we simply have to print the matrix as such.
If there is nuthing to print print a space.
For example if input is

Code: Select all

aaa                 aaa
                    aaa
aaa
         aaa
                 a
aaaaaaaaaaaaaaaaaaaaaaa
Output is :
where * denotes a space

Code: Select all

a**a*a
a**a*a
a**a*a
a*****
a*****
a*****
a*****
a*****
a*****
a*a***
a*a***
a*a***
a*****
a*****
a*****
a*****
a*****
aa****
a*****
a*****
a***aa
a***aa
a***aa

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 490 : why WA with this simple looking problem?

Post by sazzadcsedu » Thu Jan 29, 2009 10:52 pm

my code P.E.
someone help

Code: Select all


                          #include<stdio.h>
                           #include<memory.h>

                              int  main()


                              


						{ 
                                int i=0,j,k;
								char str[100][100];
								int flag[100];

							memset(flag,0,sizeof(flag));

                             	//freopen("input.txt","r",stdin);
                             	//freopen("output.txt","w",stdout);
                          
                                
								//len=0;
						   while(gets(str[i]))

						   { 
                                
								 
							       
								   i=i+1;
								   

						   } 

						/*  for(j=0;j<i;j++)
                            
						   { 
							   puts(str[j]) ;

						   }

						  */
						   
						      for(k=0;k<100;k++)
								{
                    
						            for(j=i-1;j>=0;j--)

									{
									if(str[j][k]=='\0')
									  flag[j]=1;

									if(flag[j]==0)
                                    printf("%c",str[j][k]);

									}
									printf("\n");
								}

                                  
								return 0;

						}
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 490 : why WA with this simple looking problem?

Post by mf » Fri Jan 30, 2009 12:42 am

PE usually means that you print (or failed to print) trailing spaces on some lines in the output.

See the input/output posted just above by fR0D, your program doesn't seem to pass it.

Also, 'char str[100][100]' isn't enough to store the input in case of max test.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

now WA: 490 - Rotating Sentences

Post by sazzadcsedu » Sat Jan 31, 2009 8:47 pm

now i got wrong answer??
i changed >-

Code: Select all

 
                                    if(flag[j]==0)
                                     printf("%c",str[j][k]);
                                    to 
                                    if(flag[j]==0)
                                    printf("%c",str[j][k]);
                                    else if(flag[j]==1)
                                        printf(" ");
                                   
so that it can print trailing space.
but WA.
anyone help??
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

zuphilip
New poster
Posts: 2
Joined: Thu Aug 06, 2009 12:08 am

Re: 490 - Rotating Sentences

Post by zuphilip » Thu Aug 06, 2009 12:20 am

Dear all,

I got a Wrong Answer on that problem, also I do not see what exactly is wrong. For simplicity I post here the code where each output line is the same length, i.e., there might be too many spaces in the end. What bothers me more is the following: the output on the command line is different from the output when I redirect it to some file. How can this happen?

Code: Select all

#include <iostream>
using namespace std;

#include <string>
#include <vector>

int main() {
    int n,max;
    string line;
    vector<string> input;
    max=0;  //maximum length of a line
    n=0;  //number of lines
    while (getline(cin,line)) {
          input.push_back(line);
          n++;
          if (line.size()>max)
             max = line.size();
    }
    for (int j=0; j<max; j++) {
      for (int i=n-1; i>=0; i--) {
          if (input[i].size()<j)
             cout << " ";
          else
              cout << input[i][j];
          }
      cout << endl;
    }
    return 0;
}
For the input file

Code: Select all

aa
b
the command line "rot-seq.exe < input" shows the right answer. But if I redirect it to a file "rot-seq.exe < input > output" then the output file looks like this

Code: Select all

ba
  
Why??

Thanks in advance,
Philipp

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 490 - Rotating Sentences

Post by mf » Thu Aug 06, 2009 8:58 am

Your programs outputs a NUL character, so what you see is probably a reaction of your terminal software to that character.

Here's what I see:

Code: Select all

/tmp$ g++ -o p p.cc
/tmp$ (echo aa; echo b) | ./p 
ba
a
/tmp$ (echo aa; echo b) | ./p | hexdump -C
00000000  62 61 0a 00 61 0a                                 |ba..a.|
00000006
/tmp$ 

zuphilip
New poster
Posts: 2
Joined: Thu Aug 06, 2009 12:08 am

Re: 490 - Rotating Sentences

Post by zuphilip » Thu Aug 06, 2009 10:21 am

Ah, thank you! I could resolve my problems with your hint. At some point I tried to write a character from the string at a position which is out of boundary. This yields the NUL-character. The corrected test for that, substitute < with <=, resolved the problem. Thus my code is now accepted.

asif_khan_ak_07
New poster
Posts: 25
Joined: Fri Apr 17, 2009 8:24 am

Re: 490 - Rotating Sentences

Post by asif_khan_ak_07 » Mon Aug 10, 2009 7:50 pm

Code: Select all

removed after AC
Last edited by asif_khan_ak_07 on Mon Sep 28, 2009 7:59 am, edited 1 time in total.

Post Reply

Return to “Volume 4 (400-499)”