Topic Description > Relay Games
Time limit: 1000ms memory limit: 256MB
Describe
Alice and Bob and several other good friends are playing the message game together. The game goes like this: First, all the players are in a row in order, Alice stands first, and Bob stands last. Then Alice would like to whisper a word to the second player, the second player quietly told the third, and the third told fourth place ... And so on, until the penultimate digit tells Bob. Two players in the message, can not let others hear, and can not use the body action to explain. Finally, Bob told everyone what he had heard, and Alice told everyone what she had thought.
As there may be some bias in the messenger process, the more players there are, the more likely Bob will be to hear the last words than Alice would have thought. What Bob hears often turns into something very funny, so everyone has fun. After several rounds of games, Alice noticed that in the two-person message, some words often mistakenly become other specific words. Alice has collected such a list of lexical transformations, and she wants to know what her words will look like when it reaches Bob, and ask you to write a program to help her. Input
Input includes multiple sets of data. The first line is an integer T that indicates how many sets of test data are available. The first row of each set of data consists of two integers N and M, representing the number of players and the length of the word conversion list, respectively. Then there is the M line, each containing two words A and b separated by a space, which means that word a must become B in the messenger. Enter data to guarantee no duplicates of a. The last line contains several words separated by a single space, representing the sentences Alice wants, with a total length of no more than 100 characters. All words contain only lowercase letters and are no more than 20 in length, and different tenses of the same word are considered to be different words. You can assume that words that are not in the list will never change. Output
For each set of test data, output a single line of "Case #c: S". Where c is the test data number, S is the sentence that Bob hears. The format of S is the same as that of Alice in the input data. Data range
1≤t≤100
Small data: 2≤n≤10, 0≤m≤10
Big data: 2≤n≤100, 0≤m≤100
Sample input
2
4 3 ship
sheep
sinking thinking
thinking sinking the ship is
sinking
ten 5
tidy tiny
Tiger Liar
tired tire
tire Bear
Liar Bear
A tidy tiger is tired
Sample output
Case #1: The sheep was thinking case
#2: A tiny-bear are bear
April 7 This year to practice a problem, is a string processing problem bar, moved to CSDN blog up. At that time the reference to learn the following link in the blog code, I follow his ideas to rewrite the code as pure C language code. It was my first time. The only time the sscanf () function is used. Http://www.cnblogs.com/liaoguifa/archive/2013/04/10/3013064.html
#include <stdio.h> #include <string.h> typedef struct{CHAR original[23];
Char final[23];
}trans;
Trans transform[102];
Char word[102][23];
int main () {int noftest,n,m,i,j,k,len,wordlen,nofword,flag,num=1;
Char originalsentence[122];
scanf ("%d", &noftest);
while (noftest--) {scanf ("%d%d", &n,&m);
GetChar ();
for (i=0;i<m;i++)/* read into the conversion dictionary/{scanf ("%s%s", transform[i].original,transform[i].final);
GetChar (); } fgets (Originalsentence,122,stdin);
/* Read the original sentence * * len = strlen (originalsentence);
if (originalsentence[len-1]== ' \ n ') originalsentence[len-1]= ', originalsentence[len]= ';
Else originalsentence[len]= ', originalsentence[++len]= ';
i=j=0;
while (I<len)/* Reads the original sentence one word into the word array/{sscanf (Originalsentence+i,%[^), word[j];
Wordlen = strlen (Word[j]);
j + +;
i + = wordlen+1;
} Nofword = J;
for (i=0;i<n-1;i++)/* One-word dictionary conversion * * {for (j=0;j<nofword;j++) {flag = 0; for (k=0;k&Lt
m;k++) {if (strcmp (word[j],transform[k].original) = = 0) {flag = 1;
Break
} if (flag) strcpy (word[j],transform[k].final);
} printf ("Case #%d:", num++);
for (i=0;i<nofword;i++) if (i==0) printf ("%s", Word[i]);
else printf ("%s", Word[i]);
printf ("\ n");
return 0; }