159 - Word Crosses

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Rossi
New poster
Posts: 20
Joined: Thu Mar 21, 2002 2:00 am
Location: Bangladesh

159 - Word Crosses

Post by Rossi » Thu Mar 21, 2002 12:35 pm

hey ,whats the tricky part with this one .i thought it was too simple to do

Code: Select all

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

int check(char *,char *,int *,int *);
void print(char *,int,char *,int,char *,int,char *,int);

int main(void)
{
register int i,j;
int p1,p2,p3,p4;
char w1[12],w2[12],w3[12],w4[12];

while(1){
if(scanf("%s %s %s %s",w1,w2,w3,w4)!=4)
  break;
if(w1[0]=='#')
  break;

if(check(w1,w2,&p1,&p2)&&check(w3,w4,&p3,&p4)){
  print(w1,p1,w2,p2,w3,p3,w4,p4);
  }
else
	printf("Unable to make two crossesn");

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

int check(char *str1,char *str2,int *pos1,int *pos2)
{
register int i,j;
int found;

for(i=found=0;str1[i]&&found==0;i++){
   for(j=0;str2[j]&&found==0;j++){
	  if(str1[i]==str2[j])
		found=1;
	  }
   }

if(found==1){
  *pos1=i-1;
  *pos2=j-1;
  }
return found;
}

void print(char *w1,int p1,char *w2,int p2,char *w3,int p3,char *w4,int p4)
{
register int i,j;
int l1,l2,l3,l4,hei,wid;
char mat[32][32];

for(i=0;i<32;i++){
   for(j=0;j<31;j++)
	  mat[i][j]=' ';
   mat[i][j]=NULL;
   }

l1=strlen(w1);
l2=strlen(w2);
l3=strlen(w3);
l4=strlen(w4);

if(p2>p4){
  for(i=0;i<l1;i++)
	 mat[p2][i]=w1[i];
  for(i=0;i<l2;i++)
	 mat[i][p1]=w2[i];

  for(i=0;i<l3;i++)
	 mat[p2][l1+3+i]=w3[i];
  for(i=0;i<l4;i++)
	 mat[p2-p4+i][l1+3+p3]=w4[i];

  hei=p2-p4+l4-1;
  wid=l1+l3+3;
  }
else{
	for(i=0;i<l3;i++)
	   mat[p4][l1+3+i]=w3[i];
	for(i=0;i<l4;i++)
	   mat[i][l1+3+p3]=w4[i];

	for(i=0;i<l1;i++)
	   mat[p4][i]=w1[i];
	for(i=0;i<l2;i++)
	   mat[p4-p2+i][p1]=w2[i];

	hei=p4-p2+l2-1;
	wid=l1+l3+3;
	}

for(i=0;i<=hei;i++){
   for(j=wid;j>=0;j--){
	  if(mat[i][j]!=' ')
		break;
	  }
   mat[i][j+1]=NULL;
   printf("%sn",mat[i]);
   }
}

ram
New poster
Posts: 30
Joined: Wed Mar 06, 2002 2:00 am
Contact:

Post by ram » Sat Mar 23, 2002 7:55 am

I have tested your program.I guess, Your program always considers second vertical string to be longer than first vertical string.

for example it gives wrong output for:

BALL CATAM BALL BA

ram
New poster
Posts: 30
Joined: Wed Mar 06, 2002 2:00 am
Contact:

Post by ram » Sat Mar 23, 2002 8:58 pm

Even I am getting wrong answer. But I have tried many test cases.Its OK with all those test cases. I am using VC++/windows.

Here is the code. Can any body help me???

Code: Select all

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

using namespace std;

int mx(int m1,int m2)
{
   if(m1<m2) return m2;
   else
	   return m1;

}

int main()
{

	//ifstream fin;
	//ofstream fout;
	string x1h,x1v,x2h,x2v;
	char x1h1[12],x1v1[12],x2h2[12],x2v2[12];
	//fin.open("temp.txt",ios::in||ios::nocreate);
	//fout.open("tempee.txt",ios::out);

	while((scanf("%s %s %s %s",x1h1,x1v1,x2h2,x2v2))==4){

		bool flag1,flag2;
		int i,j,h1,v1,h2,v2,k1,k2;
        x1v=x1v1;
		x2v=x2v2;
		x1h=x1h1;
		x2h=x2h2;
        if(x1h=="#") break;
		flag1=flag2=false;

		for(i=0;i<x1h.length();i++)
			for(j=0;j<x1v.length();j++)
				if(x1h[i]==x1v[j])
				{	if(!flag1)
				{
					h1=i;
					v1=j;
					flag1=true;
				}
				}

		for(i=0;i<x2h.length();i++)
			for(j=0;j<x2v.length();j++)
				if(x2h[i]==x2v[j])
				{	if(!flag2)
				{
					h2=i;
					v2=j;
					flag2=true;
				}
				}

     if(!flag2&&!flag2)
	 {
       cout<<"Unable to make two crosses"<<endl<<endl;

	 }
	 else
	 {
		 k1=x1v.length();
		 k2=x2v.length();

	  if(v1<v2)
	  {
         for(i=0;i<(v2-v1);i++)
			 x1v= " "+x1v;


	  }
	  else
	  {
              for(i=0;i<(v1-v2);i++)
			     x2v= " "+x2v;
	  }
      
	  if((k1-v1)<(k2-v2))
	  {
		  int a,b;
		  a=(k2-v2);
		  b=(k1-v1);
         for(i=0;i<(a-b);i++)
			 x1v= x1v+" ";


	  }
	  else
	  {
		  if((k1-v1)>(k2-v2))
		  {
			   int a,b;
		     a=(k2-v2);
		     b=(k1-v1);
              for(i=0;i<(b-a);i++)
			    x2v= x2v+" ";
		  }
	  }


	  for(i=0;i<x1v.length();i++)
	  {
         string printer;
		 printer="";
       if(i!=mx(v1,v2))
	   {
		   int a;

		 for(a=0;a<h1;a++)
		 {
            printer+=" ";
			
		 }
		 printer+=x1v[i];
		 for(a=0;a<x1h.length()-h1;a++)
			 printer+=" ";

		 printer+="  ";
        for(a=0;a<h2;a++)
		 {
            printer+=" ";
			
		 }
		 printer+=x2v[i];
		 for(a=0;a<(x2h.length()-h2)-1;a++)
			printer+=" ";


	   }
	   else
	   {
          printer=x1h+"   "+x2h;
	   }

	   cout<<printer.c_str()<<endl;
	  }
	 cout<<endl;
  	 }
	}
	return 0;
}

<font size=-1>[ This Message was edited by: ram on 2002-03-24 22:15 ]</font>

C8H10N4O2
Experienced poster
Posts: 137
Joined: Wed Feb 27, 2002 2:00 am
Location: Pasadena, CA

Post by C8H10N4O2 » Sun Mar 24, 2002 6:15 am

Please put tags around your code. Otherwise, it is hard to read.

Rossi
New poster
Posts: 20
Joined: Thu Mar 21, 2002 2:00 am
Location: Bangladesh

Post by Rossi » Sun Mar 24, 2002 5:02 pm

Finally i've got it right.
I'd problems with the print functions .it discarded some prtion of the output when the second cross was longer

Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

wa

Post by Caesum » Sun May 05, 2002 2:40 pm

I keep getting WA on this and I am not sure why. Here is some sample input and output from my program:

Input

Code: Select all

MATCHES CHEESECAKE PICNIC EXCUSES
CHEESECAKE MATCHES EXCUSES PICNIC
PEANUT BANANA VACUUM  GREEDY
PICNIC EXCUSES MATCHES CHEESECAKE 
A A B B
STRINGA STRINGB STRINGC STRINGD
ABCDEF GHIJKF ABCDEF GHIJKF
AABBCC DDFFBBSS KHGKHD DFHGDKJGH
PREFECT DEFER DEFER PREFECT
DEFER PREFECT PREFECT DEFER
BALL CATAM BALL BA
#
Ouput Note - these do line up properly until i post it here!!!!!!

Code: Select all

 C
 H
 E
 E
 S
 E          E
 C          X
MATCHES   PICNIC
 K          U
 E          S
            E
            S

M
A              P
T              I
CHEESECAKE   EXCUSES
H              N
E              I
S              C

Unable to make two crosses

          C
          H
          E
          E
          S
  E       E
  X       C
PICNIC   MATCHES
  U       K
  S       E
  E        
  S

A   B

STRINGA   STRINGC
T         T
R         R
I         I
N         N
G         G
B         D

     G        G
     H        H
     I        I
     J        J
     K        K
ABCDEF   ABCDEF

         D
  D      F
  D      H
  F      G
  F      D
AABBCC   KHGKHD
  B      J
  S      G
  S      H

 D
 E
 F         P
 E         R
PREFECT   DEFER
           F
           E
           C
           T

         D
         E
 P       F
 R       E
DEFER   PREFECT
 F        
 E
 C
 T

 C
BALL   BALL
 T     A
 A      
 M

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann » Mon May 06, 2002 1:15 am

Other than the lineup trouble of the board your output is correct.

Admins (or whoever knows): Is there a bbcode tag that supports preformatted text, like the pre-tag of html? If not, could you perhaps add one?

Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum » Mon May 06, 2002 11:27 am

can you think of anything other cases which might cause difficulty ?

Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum » Mon May 06, 2002 6:34 pm

never mind, i discovered it. I was printing some extra spaces on occasional lines at the end of the lines..... AC now :)

fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc » Tue May 07, 2002 8:38 pm

Stefan Pochmann wrote:Is there a bbcode tag that supports preformatted text, like the pre-tag of html? If not, could you perhaps add one?
AFAIK, there is no such tag in phpBB. I could try to add it but not before July because I'm quite busy now (final exams...)
Best regards,

Fernando N

pc5971
New poster
Posts: 34
Joined: Mon Aug 05, 2002 11:24 am
Contact:

159 - Word Crosses

Post by pc5971 » Fri Aug 09, 2002 10:13 am

I posted this program but I received an WA. Can somebody give me an exemple that make my program to go wrong???

[pascal]
cut it
[/pascal]

monika
New poster
Posts: 13
Joined: Tue Jul 23, 2002 9:45 am

Post by monika » Tue Oct 22, 2002 6:36 am

Hi,

I'm getting RE (SIGSEGV) for 159. Could someone please give me some hint....
I've tried with numerous testcases at my end.

Code: Select all

#include <stdio.h>
void main()
{
   char leadPairPossible(char*, char*);
   void printCrosses(char, char*, char*, char, char*, char*);
   char c1, c2;

   char w1[11], w2[11], w3[11], w4[11];
   while( (scanf("%s %s %s %s", w1, w2, w3, w4)) == 4 )
   {
        if(w1[0] == '#')
           break;
        w1[10] = w2[10] = w3[10] = w4[10] = '\0';
        c1=leadPairPossible(w1, w2);
        c2=leadPairPossible(w3, w4);
        if( (c1 != '\0') && (c2 != '\0'))
        {
            printCrosses(c1, w1, w2, c2, w3, w4);
        }
        else
           printf("Unable to make two crosses\n\n");
   }
}

char leadPairPossible(char* word1, char* word2)
{
   int i;
   for( i=0; i<strlen(word1); i++ )
   {
       if( strchr(word2, word1[i]) != NULL )
          return word1[i];
   }
   return '\0';
}

void printCrosses(char c1, char* str1, char* str2, char c2, char* str3, char* str4)
{
   char matrix[19][25];
   int t1, t2, t3, t4;
   int i,j,k;
   int set;

   char* s1 = strdup(str1);
   char* s2 = strdup(str2);
   char* s3 = strdup(str3);
   char* s4 = strdup(str4);
   char* tmp;

   char ch1[2], ch2[2];
   ch1[0] = c1;
   ch1[1] = '\0';
   ch2[0] = c2;
   ch2[1] = '\0';

   memset(matrix, ' ', sizeof(matrix));

   tmp = strtok(s1, ch1);
   t1 = strlen(tmp);
   if( str1[0] == c1 )
     t1=0;

   tmp = strtok(s2, ch1);
   t2 = strlen(tmp);
   if( str2[0] == c1 )
     t2=0;

   tmp = strtok(s3, ch2);
   t3 = strlen(tmp);
   if( str3[0] == c2 )
     t3=0;

   tmp = strtok(s4, ch2);
   t4 = strlen(tmp);
   if( str4[0] == c2 )
     t4=0;


   /*Fill in 1st word */
   i=(t4>t2)? t4:t2;
   for(j=0; j<strlen(str1); j++)
      matrix[i][j] = str1[j];

   /*Fill in 2nd Word */
   i = (i-t2);
   for(j=t1, k=0; k<strlen(str2); k++,i++)
      matrix[i][j] = str2[k];

   /*Fill in 3rd word */
   i=(t4>t2)? t4:t2;
   for(j=(strlen(str1)+3), k=0; k<strlen(str3); k++, j++)
      matrix[i][j] = str3[k];

   /*Fill in 4th word */
   for(i=(i-t4), j=(strlen(str1)+3+t3), k=0; k<strlen(str4); k++, i++)
      matrix[i][j] = str4[k];


   /*Remove all spaces from the end */
   for(i=0; i<19; i++)
   {
      set=0;
      for(j=23;j>=0;j--)
      {
         if(matrix[i][j] != ' ')
         {
             matrix[i][j+1] = '\0';
             set=1;
             break;
         }
      }
      if(!set) matrix[i][0] = '\0';
   }

   /*Print the strings */
   for(i=0; i<19; i++)
      if( strcmp(matrix[i], "") != 0 )
         printf("%s\n", matrix[i]);

   printf("\n");
}

ivec
New poster
Posts: 8
Joined: Tue May 27, 2003 2:41 pm
Contact:

Technical suggestion to C/C++ coders working on #159

Post by ivec » Tue Jul 01, 2003 11:18 pm

I see a lot of complicated output code in this thread.

You may want to consider the following formatting string for printf:

Code: Select all

 printf("%*c\n", col_index , c );
This prints character 'c' in column 'col_index' after the current position, by inserting leading spaces. The * instructs the formatter to pick the field width from an int parameter.

And in a similar fashion:

Code: Select all

 printf("%*c%*c\n", col0,c0,col1-col0,c1);
... prints c0 in column col0 and c1 in column col1.

With that, the whole output code can be written in 7 lines...

hth[/java]

Experimenter
Learning poster
Posts: 76
Joined: Thu Mar 13, 2003 5:12 am
Location: Russia

Post by Experimenter » Mon Jun 21, 2004 7:50 am

hi,everybody!
i cannot figure out why I get WA. it seems I've done everything OK, but still.strange.
help me, please.
[cpp]
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#define max(a,b) (a > b) ? a : b
#define min(a,b) (a < b) ? a : b


using namespace std;
struct Point
{
bool bOk;
int x,y;
Point(int x_=0,int y_=0,bool b=false):x(x_),y(y_),bOk(b)
{ return; }
Point(const Point &p) { x = p.x; y = p.y; bOk = p.bOk; }
~Point() { return; }
void operator = (const Point &p) { x = p.x; y = p.y; bOk = p.bOk; }
};

typedef vector <string> VSTRING;
typedef vector <Point > POINTS;

enum{END,CONTINUE};
int ReadInput(VSTRING &words)
{
char word[255];

scanf("%s",word);
if(word[0] == '#' && strlen(word) == 1) return END;
words.push_back(string(word));
for(int i = 1; i < 4; i++)
{
scanf("%s",word);
words.push_back(string(word));
}
return CONTINUE;
}

void WriteOutput(VSTRING &words, POINTS &p)
{
if(p.size() < 2)
{
printf("Unable to make two crosses\n\n");
return;
}
Point &p1 = p[0];
Point &p2 = p[1];
int u1 = p1.y, u2 = p2.y;
int U = max(u1,u2);
int U1 = U - u1, U2 = U - u2;
int l1 = words[1].length() - u1 - 1,
l2 = words[3].length() - u2 - 1;
int L = max(l1,l2);
int Y = U + L + 1;
int x1 = p1.x, x2 = words[0].length() + 3 + p2.x;
int X = words[0].length() + words[2].length() + 3;
int k1 = 0, k2 = 0;
for(int i = 0; i < Y; i++)
{
if(i != U)
for(int j = 0; j < X; j++)
{
if(j == x1 && i >= U1)
printf("%c",words[1][k1++]);
else
if(j == x2 && i >= U2)
{
printf("%c",words[3][k2++]);
break;
}
else printf(" ");
}
else
{
printf("%s %s",words[0].c_str(),words[2].c_str());
k1++,k2++;
}
printf("\n");
}
printf("\n");
}

Point FindPos(string &word1,string &word2)
{
int i,j;
for(i = 0; i < word1.size(); i++)
{
for(j = 0; j < word2.size();j++)
{
if(word1 == word2[j])
return Point(i,j,true);
}
}

return Point(0,0,false);
}
POINTS Solve(VSTRING &words)
{
POINTS points(0); points.reserve(2);
Point p;
p = FindPos(words[0],words[1]);
if(!p.bOk) return points;
points.push_back(p);

p = FindPos(words[2],words[3]);
if(!p.bOk) return points;
points.push_back(p);

return points;
}

int main()
{
bool bDontFinish = true;
while(bDontFinish)
{
VSTRING words(0);
words.reserve(4);
if(END == ReadInput(words))
{
bDontFinish = false;
}
else
{
POINTS points = Solve(words);
WriteOutput(words,points);
}
}
return (0);
}
[/cpp]
it would be great if you replied this post. really.

dootzky
New poster
Posts: 36
Joined: Tue Apr 12, 2005 12:20 am
Location: belgrade, serbia (ex yugoslavia)
Contact:

159 WA why?

Post by dootzky » Mon Jun 06, 2005 3:49 pm

i do the following:

- take 4 words
- find out which one of 2 vertical words is longer
- form a array of chars (polje[20][20])
- fill it very carefully with spaces and letters of every word
- print out that array

here is my code, everything seems to be in order, did i miss somethin'?

cheers,
dootzky

Code: Select all

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

	int i,j,a,b,zs1,zs2,zs3,zs4,x2,y1,y2;
	char polje[30][30];
	char rec1[20],rec2[20],rec3[20],rec4[20];

int gimme_letter1 (char ajde1[20],char ajde2[20]) {
	for (i=0;i<(signed)strlen(ajde1);i++) 
		for (j=0;j<(signed)strlen(ajde2);j++) 
			if (ajde1[i]==ajde2[j]) return i;

	return 666;
}

int gimme_letter2 (char ajde1[20],char ajde2[20]) {
	for (i=0;i<(signed)strlen(ajde1);i++) 
		for (j=0;j<(signed)strlen(ajde2);j++) 
			if (ajde1[i]==ajde2[j]) return j;

	return 666;
}

void main () {

	while (1) {
start:
		scanf("%s",&rec1);
		if (rec1[0]=='#') break;
		scanf("%s%s%s\n",&rec2,&rec3,&rec4);

		zs1 = gimme_letter1 (rec1,rec2);
		if (zs1 == 666) goto shit;

		zs2 = gimme_letter2 (rec1,rec2);
		if (zs2 == 666) goto shit;

		zs3 = gimme_letter1 (rec3,rec4);
		if (zs3 == 666) goto shit;

		zs4 = gimme_letter2 (rec3,rec4);
		if (zs4 == 666) goto shit;

		goto output;

shit:
		cout << "Unable to make two crosses\n\n";
		goto start;

output:

		// odavde OMG OUTPUT

		for (i=0;i<20;i++) for (j=0;j<20;j++) polje[i][j]='*';

		x2 = strlen(rec1) + 3 + zs3;
		
		if (zs2 > zs4) { y1=0; y2=zs2-zs4; }
		else { y2=0; y1=zs4-zs2; }

		// popuni tabelu sa dve uspravne reci...
		for (i=y1;i<(y1+(signed)strlen(rec2));i++)  // space-ovi za 1. rec
			for (j=0;j<zs1;j++) polje[i][j]=' ';

		for (i=y2;i<(y2+(signed)strlen(rec4));i++)  // space-ovi za 2. rec
			for (j=0;j<x2;j++) polje[i][j]=' ';

		a=0;
		for (i=y1;i<(y1+(signed)strlen(rec2));i++)
			if (a<(signed)strlen(rec2)) polje[i][zs1]=rec2[a++];

		a=0;
		for (i=y2;i<(y2+(signed)strlen(rec4));i++)
			if (a<(signed)strlen(rec4)) polje[i][x2]=rec4[a++];

		// dodavanje i dve vertikalne rec...

		if (zs2 > zs4) b=zs2;
		else b=zs4;

		a=0;
		for (i=0;i<(signed)strlen(rec1);i++) polje[b][i]=rec1[a++];

		polje[b][strlen(rec1)]=' ';
		polje[b][strlen(rec1)+1]=' ';
		polje[b][strlen(rec1)+2]=' ';

		a=0;
		for (i=(signed)strlen(rec1)+3;i<(signed)strlen(rec1)+3 + (signed)strlen(rec3);i++)
			polje[b][i]=rec3[a++];

		// output polja..
		a=1;
		for (i=19;i>0;i--) 
			if (a==1) for (j=0;j<20;j++) if (polje[i][j]!='*') { b=i; a=0; break;}
		
		for (i=0;i<b+1;i++) {
			for (j=0;j<20;j++) if (polje[i][j]!='*') cout << polje[i][j];
			cout << endl;
		}

		cout << endl;

		

	} // kraj while
}

Post Reply

Return to “Volume 1 (100-199)”