来源:http://acm.hdu.edu.cn/showproblem.php?pid=1075
运用map处理问题
具体见代码注释
#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
#include <ctype.h>
using namespace std;
map<string,string> m;
string s;
int search(string key) //用来判断map里是否存在key值
{
map<string,string>::iterator it=m.find(key);
if( it != m.end() )
return 1;
return 0;
}
int main()
{
string start;
string s1,s2,temp,t;//temp用来储存book部分
int i,j,len,k;
cin>>start;
while(cin>>s1>>s2)
{
if(s1=="END")break;
m.insert(make_pair(s2,s1));
}
i=0; temp="";
getchar();
while(getline(cin,s))//边循环边把内容加入temp ,用 gets读char*效率更好
{
if(s=="END")break;
temp+=s;temp+='\n';
}
for( j=0;j<temp.length();j++)//输出
{
if(!isalpha(temp[j]))
{
//t+='\0';
if(search(t)) printf("%s%c",m[t].c_str(),temp[j]);
else printf("%s%c",t.c_str(),temp[j]);
t=""; continue;
}
t+=temp[j];
}
//cout<<endl;
return 0;
}