首页 > ACM题库 > HDU-杭电 > Hdu 1462 Word Crosses-模拟[解题报告] C++
2013
12-11

Hdu 1462 Word Crosses-模拟[解题报告] C++

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 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 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.

样例输入:

MATCHES CHEESECAKE PICNIC EXCUSES
PEANUT BANANA VACUUM  GREEDY
A  VANISHING   LETTER TRICK
#

样例输出:

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

Unable to make two crosses

V
A   LETTER
N     R
I     I
S     C
H     K
I
N
G


不用复杂的算法,但是有比较复杂的处理。

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
using namespace std;
#undef _DEBUG
#ifdef _DEBUG
#define PRINT_VAR() {/
    printf("%3d: p = %x, q = %x/n", __LINE__, p, q);/
}
#endif
char ans[30][50];
char a[100], b[100], c[100], d[100];
int main()
{
    int cs = 1;

    while(scanf("%s%s%s%s", a, b, c, d) == 4)
    {
        memset(ans, 0, sizeof(ans));
        strcpy(ans[10], a);
        int offset = strlen(a) + 3;
        strcpy(ans[10] + offset, c);
        char *p = a;
        while(*p)
        {
            char *q = b - 1;
            while(*++q && *q != *p) {}
            if(*q)
            {
                int i = 1;
                char *t = q;
                while(--t >= b)
                    ans[10 - i++][p - a] = *t;
                t = q;
                i = 1;
                while(*++t)
                    ans[10 + i++][p - a] = *t;
                break;
            }
#ifdef _DEBUG
            PRINT_VAR();
#endif
            ++p;
        }
        bool isSolvable = ((*p) ? true : false);
        p = c;
        while(*p && isSolvable)
        {
            char *q = d - 1;
            while(*++q && *q != *p) {}
            if(*q)
            {
                int i = 1;
                char *t = q;
                while(--t >= d)
                    ans[10 - i++][p - c + offset] = *t;
                t = q;
                i = 1;
                while(*++t)
                    ans[10 + i++][p - c + offset] = *t;
                break;
            }
#ifdef _DEBUG
            PRINT_VAR();
#endif
            ++p;
        }

        isSolvable = isSolvable && (*p);

        if(cs++ != 1)
            printf("/n");

        if(! isSolvable)
        {
            printf("Unable to make two crosses/n");
            continue;
        }

        int r = 0;
        while(r < 30)
        {
            bool lineIsEmpty = true;
            int c = 0;
            while(c < 50)
            {
                int spaces = 0;
                while(c < 50 && !ans[r][c])
                {
                    ++c;
                    ++spaces;
                }
                if(c >= 50)
                {
                    if(! lineIsEmpty)
                        printf("/n");
                    ++c;
                    continue;
                }

                lineIsEmpty = false;

                while(spaces-- > 0)
#ifdef _DEBUG
                    printf(".");
#else
                    printf(" ");
#endif
                printf("%c", ans[r][c]);
                ++c;
            }
            ++r;
        }
    }
    return 0;
}