[UVA] 159 - Word Crosses
Word Crosses
Word Crosses |
A word cross is formed by printing a pair of words, the first horizontally and the second vertically, so that they share a common letter. A leading word cross is one where the common letter is as near as possible to the beginning of the horizontal word, and, for this letter, as close as possible to the beginning of the vertical word. Thus DEFER and PREFECT would cross on the first 'E' in each word, PREFECT and DEFER would cross on the 'R'. Double leading word crosses use two pairs of words arranged so that the two horizontal words are on the same line and each pair forms a leading word cross.
Write a program that will read in sets of four words and form them (if possible) into double leading word crosses.
Input
Input will consist of a series of lines, each line containing four words (two pairs). A word consists of 1 to 10 upper case letters, and will be separated from its neighbours by at least one space. The file will be terminated by a line consisting of a single #.
Output
Output will consist of a series of double leading word crosses as defined above. Leave exactly three spaces between the horizontal words. If it is not possible to form both crosses, write the message `Unable to make two crosses'. Leave 1 blank line between output sets.
Sample input
MATCHES CHEESECAKE PICNIC EXCUSES PEANUT BANANA VACUUM GREEDY #
Sample output
C H E E S E E C X MATCHES PICNIC K U E S E S Unable to make two crosses
題目感覺就很繁瑣,前兩個做一個 Word Cross,後兩個也是。
然後水平的要對齊,有可能後面那個比較低,就要對齊後者。
再者是交叉的位置越小越好。
不要輸出多餘的空白就成了。
#include <stdio.h>
#include <string.h>
int main() {
char h1[50], h2[50], v1[50], v2[50];
int i, j, k;
int first = 0;
while(scanf("%s %s %s %s", h1, v1, h2, v2) == 4) {
if(first) puts("");
first = 1;
int posh1 = -1, posv1 = -1;
for(i = 0; h1[i] && posh1 < 0; i++)
for(j = 0; v1[j] && posv1 < 0; j++)
if(h1[i] == v1[j])
posh1 = i, posv1 = j;
int posh2 = -1, posv2 = -1;
for(i = 0; h2[i] && posh2 < 0; i++)
for(j = 0; v2[j] && posv2 < 0; j++)
if(h2[i] == v2[j])
posh2 = i, posv2 = j;
char g[105][105];
memset(g, ' ', sizeof(g));
if(posh1 < 0 || posh2 < 0) {
puts("Unable to make two crosses");
continue;
}
if(posv1 >= posv2) {
for(i = 0; v1[i]; i++)
g[i][posh1] = v1[i];
for(j = 0; h1[j]; j++)
g[posv1][j] = h1[j];
int x = posv1, y = j;
for(i = y+3, j = 0; h2[j]; j++, i++)
g[x][i] = h2[j];
for(j = 0, i = x-posv2; v2[j]; j++, i++)
g[i][y+3+posh2] = v2[j];
} else {
for(i = 0; h1[i]; i++)
g[posv2][i] = h1[i];
int x = i;
for(i += 3, j = 0; h2[j]; i++, j++)
g[posv2][i] = h2[j];
for(i = 0, j = posv2-posv1; v1[i]; i++, j++)
g[j][posh1] = v1[i];
for(i = 0, j = 0; v2[i]; i++, j++)
g[j][x+posh2+3] = v2[i];
}
for(i = 0; i < 100; i++) {
j = 99;
while(j >= 0 && g[i][j] == ' ') j--;
if(j < 0) break;
for(k = 0; k <= j; k++)
putchar(g[i][k]);
puts("");
}
}
return 0;
}