编程之美~~传话游戏

 1 #include <iostream>
 2 #include <map>
 3 #include <vector>
 4 #include <sstream>
 5 #include <string>
 6 using namespace std;
 7 
 8 int main(){
 9     int T;
10     cin>>T;
11     for(int iCase = 1; iCase <= T; iCase ++){
12         int N,M;
13         cin>>N>>M;
14         map<string,string> word;
15         for(int i = 0; i < M; i ++ ){
16             string a,b;
17             cin>>a>>b;
18             word[a]=b;
19         }
20         vector<string> game;
21         string a;
22         getline(cin,a);
23         getline(cin,a);
24 
25         stringstream sst(a);
26         while(sst>>a) game.push_back(a);
27 
28         for(int i = 0; i < N-1; i ++){
29             for(int j = 0; j < game.size(); j++ ){
30                 if(word.find(game[j]) != word.end())
31                     game[j] = word[game[j]];
32             }
33         }
34         cout<<"Case #"<<iCase<<":";
35         for(int i = 0; i <game.size(); i ++ ) cout<<" "<<game[i];
36         cout<<endl;
37     }
38     return 0 ;
39 }

 

posted @ 2013-04-06 18:55  OpenSoucre  阅读(327)  评论(2编辑  收藏  举报