UVA159 HDU1462 Word Crosses【输出】

708 篇文章 18 订阅
12 篇文章 0 订阅

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 <bits/stdc++.h>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值