網頁

2014年1月10日 星期五

UVa 10282 & POJ 2503 Babelfish

想法:
  本題可直接使用STL的map來做string與string的對應,用printf而不用cout可提升效率,因此使用c_str()將c++字串轉為c字串(POJ實測是985ms與766ms)。



#include <cstdio>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string,string>m;
char line[200];
while(gets(line)){
if (line[0]=='\0') break;
char a[50],b[50];
sscanf(line,"%s %s",a,b);
m[b]=a;
}
while(gets(line)){
if (line[0]=='\0') break;
if (m[line]=="\0") //map如果沒對應到,其內容為"\0"
printf("eh\n");
else
printf("%s\n",m[line].c_str()); //把C++字串換回C字串
}
return 0;
}

沒有留言:

張貼留言