【map运用】POJ-2503 Babelfish

字符读取

Babelfish

注意事项

1.输入处理。输入字典时,输入的是未知行数、中间带一个空格的字符串,以空行代表输入字典结束。

样例输入:

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

读取代码如下:

char s[maxn];
char now[maxn], f[maxn];
map<string, string> mp;
while (gets(s) && s[0] != '\0') {
    
    
    sscanf(s, "%s %s", now, f);
    mp[f] = now;
}

注意sscanf() 函数的运用。

参考代码

const int maxn = 50;

using namespace std;

int readint() {
    
    
    int x; scanf("%d", &x); return x;
}


int main() {
    
    
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//   freopen("output.txt", "w", stdout);
#endif
    char s[maxn];
    char now[maxn], f[maxn];
    map<string, string> mp;
    while (gets(s) && s[0] != '\0') {
    
    
        sscanf(s, "%s %s", now, f);
        mp[f] = now;
    }
    while (~scanf("%s", s)) {
    
    
        printf("%s\n", mp.count(s) ? mp[s].c_str() : "eh");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Encore47/article/details/113135882
今日推荐