Automatic Poetry
“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
题目大意:
输入一个数,表示有几组数据。每组数据包括两行。第一行输入的字符串格式形如:s1<s2>s3<s4>s5。每个si都有可能为空,小写字母或空格。
第二行为小写字母或空格,以...标记结束。
输出第一行为s1s2s3s4s5,第二行为(输入的第二行...之前的原样输出)s4s3s2s5。
思路:
因为其他地方都是原样输出,所以只做将<s1>s2<s3>s2变为s3s2s1s4。(原先的s2为s1,s3为s2……)
1 #include<stdio.h> //这道这么简单的题把我气回了家,总是wrong answer 2 #include<string.h> 3 4 int main() 5 { 6 int n,len1,len2,s1,s2,s3,s4,i,flag; 7 char a[105],b[105]; 8 scanf("%d", &n); 9 getchar(); 10 while (n--) 11 { 12 gets(a); 13 gets(b); 14 flag = 0; 15 len1 = strlen(a); 16 len2 = strlen(b); 17 for (i = 0; i < len1; i++)//第一行 18 { 19 if (a[i] != '<' && a[i] != '>') 20 printf("%c", a[i]); 21 } 22 printf(" "); 23 for (i = 0; i < len1; i++) 24 { 25 if (a[i] == '<' && flag == 0) //一开始我想的是如果遇到<,在遇到>前就把这个单词输出。其实更好的做法是做标记,记下此时i的值 26 { 27 s1 = i; //这样有条理、简单许多了就,不像我弄得那么麻烦 28 } 29 else if (a[i] == '>' && flag == 0) 30 { 31 s2 = i; //学会记下此时i的值,就是本题的收获!! 32 flag = 1; 33 } 34 else if (a[i] == '<' && flag == 1) 35 { 36 s3 = i; 37 } 38 else if (a[i] == '>' && flag == 1) 39 { 40 s4 = i; 41 } 42 } 43 for (i = 0; i < len2; i++) 44 { 45 if (b[i] == '.') //这块也比较巧妙 46 { 47 for (i = s3 + 1; i < s4; i++) //这样输出s4s3s2s5岂不是方便多了? 48 { 49 printf("%c", a[i]); 50 } 51 for (i = s2 + 1; i < s3; i++) 52 { 53 printf("%c", a[i]); 54 } 55 for (i = s1 + 1; i < s2; i++) 56 { 57 printf("%c", a[i]); 58 } 59 for (i = s4 + 1; i < len1; i++) 60 { 61 printf("%c", a[i]); 62 } 63 break; 64 } 65 else 66 printf("%c", b[i]); 67 } 68 printf(" "); 69 } 70 return 0; 71 }