Microsoft Challenge's Message Game Problem _ project Summary

Source: Internet
Author: User
Tags assert tidy

Time: 2014.01.26

Location: Second Floor, base

-------------------------------------------------------------------First, brief

Today, I saw the title of the National Challenge of Microsoft programming, so take to try, found still very basic, as long as the foundation is solid, I want to write the program should still be very reliable.

-------------------------------------------------------------------
Second, the topic

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

10 5

Tidy Tiny

Tiger Liar

Tired Tire

Tire Bear

Liar Bear

A tidy tiger is tired

Sample output

Case #1: The Sheep is thinking

Case #2: A tiny the bear are bear

-------------------------------------------------------------------
Third, analysis

Although the topic is simple, but the basic knowledge involved is still many, there are many details of places to pay attention to, such as restrictions on conditions, check the legality of the input string and so on. The following analysis:

3.1 First, we want to check the legality of the data range, we will represent the number of parameters N and representative of the number of conversion list of M to check together, the code is as follows:

BOOL Isparameterleagal (char N, char m) {
	//postcondition:check the parameter ' s Leagal and return-the result
	bool I S_n_m_leagal = ((n <= && n >= 2) && (M <= && m >= 0)) | |
						 ((n <= && n >= 2) && (M <= && m >= 0));
	return is_n_m_leagal;
}
Why return a bool value, I was thinking of a combination of other data testing together to make an assertion, and here is only a part of the validation of the input, so here after the test returns a bool value, only to indicate the legality of the two parameters to make a judgment.

3.2 Second, there are requirements for input statements in the request, so check here as well:

BOOL Isinputstringleagal (const string& input_str) {
	//postcondition:check the input sttring ' s leagal and return T
	//library Facilities Used:sstream
	if (input_str.length () >100) return
		false;
	Istringstream record (INPUT_STR);
	string Word;
	While [record >> Word] {
		if (Word.length () >) return
			false;
		for (auto C:word)
			if (!islower (c)) return
				false;
	}
	return true;
}
Here are some ways to find short-circuit, that is, if found illegal, return in time to False
3.3 Here is the input data Processing section, including basic information, such as how many test examples. n indicates that the number of people involved in the message involves N-1, so it is changing in an n-1 cycle.
String inputdataprocess () {
	//postcondtion:input information and data process
	//library facilities Used:assert , map
	unsigned N, M;
	CIN >> N >> M;
	ASSERT (Isparameterleagal (N, M));
	Map<string, string> Transfer_map;
	while (M!= 0) {
		string origin_word, transfer_word= "";
		CIN >> Origin_word >> Transfer_word;
		Transfer_map.insert ({origin_word, transfer_word});
		m--;
	}
	string transfer_words;
	GetChar ();
	Getline (CIN, transfer_words);
	ASSERT (Isinputstringleagal (transfer_words));
	while (N!= 1) {
		transfer_words = transferwords (Transfer_words, transfer_map);
		n--;
	}
	return transfer_words;
}
Here to say is the conversion list of the construction, I used a map container, mainly for the convenience of the back to find, improve efficiency. Also a place to pay attention to when the use of GetChar (), here is to eat the carriage return, at the beginning in this place tangled for a long time, because the getline () function has been reading I want a line of string, but an empty string, and I test the use of the Getline function alone is not a problem, It was a strange idea, and then I knew the CIN string, it will still place the line break in the stream, so getline reads the line break immediately, and it ends with a line break, so it gets an empty string, and GetChar eats the line break. This code I think can also be optimized, so that the code looks more concise structure elegant, such as the creation of the transformation of the part of the table can be completely independent to become a module, the readability is higher.

3.4 The following section is a string conversion function to simulate the call between two people, in this process some words will be in accordance with the format specified in the conversion list to do the corresponding conversion, we used the Sstringstream stream object, the string with a Istringstream object binding, Query each word individually, the conversion of the transformation, the reservation should not be converted.

String transferwords (const string& input_str,const map<string,string>& given_map)
{
	// Postcontion:when A communicate occured between two persons,the words would transfer 
	//             according the transfer list . 
	Istringstream record (INPUT_STR);
	String word, transfer_words= "";
	while (record >> word) {
		Auto it = given_map.find (word);
		if (it!= given_map.end ())
			transfer_words = = ("" +it->second);
		else
			transfer_words + = ("" + word);
	}
	return transfer_words;
}

3.5 is a part of the main function, in this section can reflect the overall structure of the program, the program for each group of tests to deal with, and then store the results after a vector, and finally go to the variable vector container, the results printed as required.

int main () {
	unsigned T;
	Cin >> T;
	ASSERT (t <= && t >= 0);
	
	Vector<string> Transfer_vec;
	while (t!=0) {
		string out_string =inputdataprocess ();
		Transfer_vec.push_back (out_string);
		t--;
	}
	unsigned count = 1;
	for (auto Str:transfer_vec)
	{
		cout << "Cade #" << Count << ": << str << endl;
  count++;
	}
	return 0;
}
-------------------------------------------------------------------
Four, complete code

#include <iostream> #include <string> #include <cassert> #include <vector> #include <map>
#include <sstream> #include <cctype> using namespace std; BOOL Isparameterleagal (char N, char m) {//postcondition:check The parameter ' s Leagal and return-the result bool Is_n_m_
						 Leagal = ((n <= && n >= 2) && (M <= && m >= 0)) | |
	((n <= && n >= 2) && (M <= && m >= 0));
return is_n_m_leagal; BOOL Isinputstringleagal (const string& input_str) {//postcondition:check The input sttring ' s leagal and return th
	E result//library Facilities Used:sstream if (Input_str.length () >100) return false;
	Istringstream record (INPUT_STR);
	string Word;
		While [record >> Word] {if (Word.length () >) return false;
	for (auto C:word) if (!islower (c)) return false;
return true; } string Transferwords (const string& Input_str,const map<string,string>& given_map) {//postcontion:when A communicate occured between two persons,the words would transfer// 
	According the transfer list.
	Istringstream record (INPUT_STR);
	String word, transfer_words= "";
		while (record >> word) {Auto it = given_map.find (word);
		if (it!= given_map.end ()) Transfer_words = = ("" +it->second);
	else transfer_words + = ("" + word);
return transfer_words;
	String inputdataprocess () {//postcondtion:input information and data process//library facilities Used:assert,map
	unsigned N, M;
	CIN >> N >> M;
	ASSERT (Isparameterleagal (N, M));
	Map<string, string> Transfer_map;
		while (M!= 0) {string origin_word, transfer_word= "";
		CIN >> Origin_word >> Transfer_word;
		Transfer_map.insert ({origin_word, transfer_word});
	m--;
	} string Transfer_words;
	GetChar ();
	Getline (CIN, transfer_words);
	ASSERT (Isinputstringleagal (transfer_words)); while (N!= 1) {transfer_words = TraNsferwords (Transfer_words, Transfer_map);
	n--;
return transfer_words;
	int main () {unsigned T;
	Cin >> T;
	
	ASSERT (t <= && t >= 0);
	Vector<string> Transfer_vec;
		while (t!=0) {string out_string =inputdataprocess ();
		Transfer_vec.push_back (out_string);
	t--;
	} unsigned count = 1;
		for (auto Str:transfer_vec) {cout << "Cade #" << Count << ": << str << Endl;
	count++;
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.