UVA159 HDU1462 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

问题链接:UVA159 HDU1462 Word Crosses
问题简述:(略)
问题分析
    这是一个输出格式的为问题,看代码,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA159 HDU1462 Word Crosses */

#include 

using namespace std;

const int M = 10;
const int N = 50;
char ans[N][N];

bool judge(char s1[], char s2[], int &p, int &q)
{
    for(int i = 0; s1[i]; i++)
        for(int j = 0; s2[j]; j++)
            if(s1[i] == s2[j]) {
                p = i;
                q = j;
                return true;
            }
    return false;
}

int main()
{
    char a[M + 1], b[M + 1], c[M + 1], d[M + 1];
    int flag = 0, p1, p2, p3, p4;
    while(~scanf("%s", a) && a[0] != '#') {
        if(flag) putchar('\n');
        flag = 1;

        scanf("%s%s%s", b, c, d);
        bool f1 = judge(a, b, p1, p2);
        bool f2 = judge(c, d, p3, p4);
        if(f1 == false || f2 == false)
            puts("Unable to make two crosses");
        else {
            int t, len = strlen(a);

            memset(ans, ' ', sizeof(ans));
            if(p2 < p4) {
                t = p4;
                for(int i = 0; b[i]; i++)
                    ans[p4 - p2 + i][p1] = b[i];
                for(int i = 0; d[i]; i++)
                    ans[i][len + p3 + 3] = d[i];
            } else {
                t = p2;
                for(int i = 0; b[i]; i++)
                    ans[i][p1] = b[i];
                for(int i = 0; d[i]; i++)
                    ans[p2 - p4 + i][len + p3 + 3] = d[i];
            }

            for(int i = 0; a[i]; i++)
                ans[t][i] = a[i];
            for(int i = 0; c[i]; i++)
                ans[t][len + i + 3] = c[i];

            for(int i = 0; i < N; i++) {
                int end = -1;
                for(int j = 0; j < N; j++)
                    if(ans[i][j] != ' ') end = j;
                for(int j = 0; j <= end; j++) putchar(ans[i][j]);
                if(end != -1) putchar('\n');
            }
        }
    }

    return 0;
}

你可能感兴趣的:(#,ICPC-UVA,#,ICPC-HDU,#,ICPC-输出流)