Uva 10361 Automatic Poetry

Problem I

Automatic Poetry

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

“Oh God”, Lara Croft exclaims, “it’s one of these dumb riddles again!”

In Tomb Raider XIV, Lara is, as ever, gunning her way through ancient Egyptian pyramids, prehistoric caves and medival hallways. Now she is standing in front of some important Germanic looking doorway and has to solve a linguistic riddle to pass. As usual, the riddle is not very intellectually challenging.

This time, the riddle involves poems containing a “Schuttelreim”. An example of a Schuttelreim is the following short poem:

Ein Kind halt seinen Schnabel nur,

wenn es hangt an der Nabelschnur.        

/*German contestants please forgive me. I had to modify something as they were not appearing correctly in plain text format*/

A Schuttelreim seems to be a typical German invention. The funny thing about this strange type of poetry is that if somebody gives you the first line and the beginning of the second one, you can complete the poem yourself. Well, even a computer can do that, and your task is to write a program which completes them automatically. This will help Lara concentrate on the “action” part of Tomb Raider and not on the “intellectual” part.

Input

The input will begin with a line containing a single number n. After this line follow n pairs of lines containing Schuttelreims. The first line of each pair will be of the form

s1<s2>s3<s4>s5

where the si are possibly empty, strings of lowercase characters or blanks. The second line will be a string of lowercase characters or blanks ending with three dots “...”. Lines will we at most 100 characters long.

Output

For each pair of Schuttelreim lines l1 and l2 you are to output two lines c1 and c2 in the following way: c1 is the same as l1 only that the bracket marks “<” and “>” are removed. Line c2 is the same as l2 , except that instead of the three dots the string s4s3s2s5 should appear.

Sample Input

3

ein kind haelt seinen <schn>abel <n>ur

wenn es haengt an der ...

weil wir zu spaet zur <>oma <k>amen

verpassten wir das ...

<d>u <b>ist

...

Sample Output

ein kind haelt seinen schnabel nur

wenn es haengt an der nabel schnur

weil wir zu spaet zur oma kamen

verpassten wir das koma amen

du bist

bu dist


TUD Programming Contest

#include<stdio.h>
#include<string.h>

int main()
{
    char list[120],list2[120];
    int T, n, i, j, len, len2, loc1, loc2, loc3, loc4, empty, flag;
    scanf("%d", &T);
    getchar();
    while(T--)
    {
        loc1 = loc2 = loc3 = 0;
        loc4 = len = empty = 0;
        flag = 0;
        memset(list, '\0', sizeof(char)*120);
        memset(list2, '\0', sizeof(char)*120); 
        fgets(list, 120, stdin);
        len = strlen(list) - 1;
        if(len == 4) {empty = 1;}
        else
        {
            if(list[0] == '<') {loc1 = 0; flag = 1;}
            for(i=0; i<len; ++i)
            {
                if(list[i] == '<' || list[i] == '>')
                {
                    if(list[i] == '<') if(flag || loc1) loc3 = i; else loc1 = i;
                    if(list[i] == '>') if(!loc2) loc2 = i; else loc4 = i;
                }
            }
        }
        
        fgets(list2, 120, stdin);
    
        len2 = strlen(list2)-1;
        if(!empty) 
        {
            for(i=0; i<len; ++i)
            {
                if(list[i] != '<' && list[i] != '>')
                printf("%c", list[i]);
            }
        } 
        printf("\n");
        for(i=0; i<len2-3; ++i) printf("%c", list2[i]); 
        if(!empty)
        { 
            for(i=loc3+1; i<loc4; ++i) printf("%c", list[i]);
            for(i=loc2+1; i<loc3; ++i) printf("%c", list[i]);
            for(i=loc1+1; i<loc2; ++i) printf("%c", list[i]);
            for(i=loc4+1; i<len; ++i) printf("%c", list[i]);
        }
        printf("\n");
        
    }
    return 0;
}

解题报告:

根据题目意思,分割记录<>的位置,要考虑到第一行仅是<><>和第二行仅是...的可能,然后根据题目要求进行输出

WA——reason:

关乎到OJ的判题系统,而本题是Standard Input & Standard Ouput,用fgets时,在考虑最后一行输入时究竟有无换行符时,认为是由文件输入,此时并没有换行符

从而WA了一次,而看到题目标题才明白了!! WA第二次的时候我事先将第一行输出了,而题目明明要求是两行输入后两行输出啊。

原文地址:https://www.cnblogs.com/liaoguifa/p/2784944.html