第九次訓練 G題

問題鏈接:Problem G

問題簡述:

火星人講的火星語小明聽不懂,於是火星人丟給小明一本字典,上面都是每個英語單詞對應的火星語,再給你一句話,讓你編寫程序對照着字典將火星語翻譯成英語(若字典中沒有對應的單詞,則輸出原話)。

問題分析:

字符串與字符串一一對應,一個map就能解決了。只需要在後續處理中注意好對空格和標點符號即可。

程序說明:

用cin輸入的時候真的不要貪快用std::ios::sync_with_stdio(false)取消同步提高cin的速度!!!經常會莫名其妙發生一些輸入錯誤導致WA,快把我坑死了,數據量大就老老實實用scanf

AC通過的C語言程序如下:

#include<iostream>
#include<cstdio>
#include<cstdlib> 
#include<algorithm>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
#include<map>
#define N 200005
using namespace std;
char st[N];
int main(){
	string ss;
	string ss2;
	map<string,string>m;
	while(cin>>ss){
		if(ss=="START"){
			continue;
		}
		if(ss=="END"){
			break;
		}
		cin>>ss2;
		m[ss2]=ss;
	//	cout<<m[ss2]<<endl;
	}
	cin>>ss;
	getchar();
	while(1){
		gets(st);
		if(strcmp(st,"END")==0){
			break;
		}
		ss="";
		int len=strlen(st);
		for(int i=0;i<len;i++){
			if(st[i]>='a'&&st[i]<='z'){
				ss+=st[i];
			}
			else{
				if(ss!=""){
					if(m.find(ss)==m.end()){
						cout<<ss;
					}
					else{
						cout<<m[ss];
					}
					cout<<st[i];
					ss="";
				}
				else{
					cout<<st[i];
					ss="";
				}
			}
		}
		cout<<endl;
	}
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章