本題可直接使用STL的map來做string與string的對應,用printf而不用cout可提升效率,因此使用c_str()將c++字串轉為c字串(POJ實測是985ms與766ms)。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
沒有留言:
張貼留言