hdu 1462 Word Crosses

23 篇文章 0 订阅
4 篇文章 0 订阅

题目地址:

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 EXCUSES PEANUT BANANA VACUUM GREEDY A VANISHING LETTER TRICK #
 

Sample Output
  
  
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

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

题解:
此题特别注意格式,可以算出各自的长度,公共字符的位置,开始输出串的位置等等都可以根据数值算出来。另外此题除了容易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 coordinate
int c1,c2,c3,c4;//the common letter 's position in own string
int hlen=3, vlen, ilen;//number of spaces, initialize length of spaces for first column
int 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);
}









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值