hdoj1075 What Are You Talking About(STL map處理字符串)

來源: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;	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章