時間限制: 1000ms 內存限制: 256MB
- 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
代碼如下:
- Case #1: the sheep is thinking
- Case #2: a tiny bear is bear
上述代碼來自:http://blog.sina.com.cn/s/blog_959bf1d30101evsx.html 另一組代碼:
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <map>
- using namespace std;
- map<string,string> mm;
- char s1[30],s2[30],s[110],*p;
- int t,m,n;
- void solve()
- {
- int i;
- string str;
- str=p;
- for(i=1;i<n;i++)
- {
- if(mm.count(str))
- str=mm[str];
- else
- break;
- }
- cout<<str;
- }
- int main()
- {
- int i,cnt;scanf("%d%*c",&t);
- for(cnt=1;cnt<=t;cnt++)
- {
- mm.clear();
- scanf("%d %d%*c",&n,&m);
- for(i=0;i<m;i++)
- {
- scanf("%s %s%*c",s1,s2);
- mm[s1]=s2;
- }
- gets(s);
- printf("Case #%d: ",cnt);
- p=strtok(s," ");
- solve();
- while((p=strtok(NULL," ")))
- {
- cout<<" ";
- solve();
- }
- cout<<endl;
- }
- return 0;
- }
來自:http://blog.csdn.net/xihuanqiqi/article/details/8763444
- #include <stdio.h>
- #include <cstring>
- #include <vector>
- #include <map>
- using namespace std;
- //字符串分割函數
- std::vector<std::string> split(std::string str,std::string pattern)
- {
- std::string::size_type pos;
- std::vector<std::string> result;
- str+=pattern;//擴展字符串以方便操作
- int size=str.size();
- for(int i=0; i<size; i++)
- {
- pos=str.find(pattern,i);
- if(pos<size)
- {
- std::string s=str.substr(i,pos-i);
- result.push_back(s);
- i=pos+pattern.size()-1;
- }
- }
- return result;
- }
- void Update(vector<string> & str,map<string,string> & w_map)
- {
- map<string,string>::iterator it;
- for(int i=0;i<str.size();i++)
- {
- it = w_map.find(str[i]);
- if(it != w_map.end())
- {
- str[i]=it->second;
- }
- }
- }
- int main()
- {
- int T;
- while(scanf("%d",&T) == 1)
- {
- for(int x=1;x<=T;x++)
- {
- int N,M;
- map<string,string> words_map;
- string s1,s2;
- string srcWords,tgtWords;
- scanf("%d%d",&N,&M);
- for(int i=1;i<=M;i++)
- {
- cin>>s1>>s2;
- words_map.insert(make_pair(s1,s2));
- }
- cin.get();//吃掉上一行換行符
- getline(cin,srcWords,'\n');
- vector<string> singleWord = split(srcWords," ");
- for(int i=1;i<=N-1;i++)
- {
- Update(singleWord,words_map);
- }
- printf("Case #%d: ",x);
- for(int i=0;i<singleWord.size()-1;i++)
- cout<<singleWord[i]<<" ";
- cout<<singleWord[singleWord.size()-1]<<endl;
- }
- }
- return 0;
- }
本文出自 “求學者Wenry” 博客,請務必保留此出處http://wenryxu.blog.51cto.com/6496375/1174144