Re: 490-Rotating sentence
Posted: Fri Nov 14, 2008 10:28 pm
Thanks kabir bhai.
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;
}
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,
.
"
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 wonder how this code got WA. it should get RE coz...the variable tem.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); }
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.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....
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;
}
Code: Select all
freopen("out.txt","w",stdout);
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.
Code: Select all
aaa aaa
aaa
aaa
aaa
a
aaaaaaaaaaaaaaaaaaaaaaa
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
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;
}
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(" ");
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;
}
Code: Select all
aa
b
Code: Select all
ba
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$