hdu 1462 Word Crosses

来源:互联网 发布:自动计算加减软件 编辑:程序博客网 时间:2024/05/22 20:33

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1462

题目描述:

Word Crosses

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 287    Accepted Submission(s): 93


Problem Description
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 EXCUSESPEANUT BANANA VACUUM GREEDYA VANISHING LETTER TRICK#
 

Sample Output
C H E E S E E C XMATCHES PICNIC K U E S E SUnable to make two crossesVA LETTERN RI IS CH KING

题意:
分别让两个串交叉输出。

题解:
此题特别注意格式,可以算出各自的长度,公共字符的位置,开始输出串的位置等等都可以根据数值算出来。另外此题除了容易WA外也很容易PE,注意不要输出多余的空格,否则很容易PE

代码:

#include<stdio.h>#include<string.h>char s1[12],s2[12],s3[12],s4[12];int l1,l2,l3,l4;int f1,f2,f3,f4;//the first letter 's position in the global coordinateint c1,c2,c3,c4;//the common letter 's position in own stringint hlen=3, vlen, ilen;//number of spaces, initialize length of spaces for first columnint print_space(int num){    for(int i=0;i<=num-1;i++) printf(" ");    return(0);}int main(){    int cases = 0;    while(scanf("%s",s1)!=EOF&&s1[0]!='#')    {        if(cases > 0) printf("\n");        int i,j;        int line=0;//the length of coordinate        scanf("%s%s%s",s2,s3,s4);        l1=strlen(s1);        l2=strlen(s2);        l3=strlen(s3);        l4=strlen(s4);        int flag1=0;//can not print result        for(i=0;i<=l1-1;i++)        {            for(j=0;j<=l2-1;j++)            {                if(s1[i] == s2[j])                {                    c1=i;                    c2=j;                    flag1=1;                    break;                }            }            if(j<=l2-1)             {                break;            }        }        int flag2=0;        for(i=0;i<=l3-1;i++)        {            for(j=0;j<=l4-1;j++)            {                if(s3[i] == s4[j])                {                    c3=i;                    c4=j;                    flag2=1;                    break;                }            }            if(j<=l4-1)             {                break;            }        }        if(flag1==1&&flag2==1)        {            if(c2 > c4)//f2=0            {                f1=c2;                f2=0;                f3=f1;                f4=f3-c4;                   line = f4+l4-1 > l2-1 ? f4+l4-1 : l2-1;            }            else            {                f4=0;                f1=c4;                f3=f1;                f2=f3-c2;                line = f2+l2-1 > l4-1 ? f2+l2-1 : l4-1;            }            vlen=l1-1-c1 + 3 + c3;            ilen=c1;            for(i=0;i<=line;i++)            {                if(i!=f1)                {                    print_space(ilen);                    if(i>=f2&&(i-f2)<=l2-1) printf("%c",s2[i-f2]);                    else printf(" ");                }                if(i!=f1)                {                    if(i>=f4&&(i-f4)<=l4-1) print_space(vlen);//PE                    if(i>=f4&&(i-f4)<=l4-1) printf("%c",s4[i-f4]);                    //else printf(" ");//PE                }                if(i==f1)                {                    printf("%s",s1);                    print_space(hlen);                    printf("%s",s3);                }                printf("\n");            }        }        else//print no answer        {            printf("Unable to make two crosses\n");        }        cases++;    }    return(0);}









0 0
原创粉丝点击