B3927 [GESP202312 四级] 小杨的字典 题解
首先考虑如何分割出每个单词,遍历整个字符串
再考虑如何替换,可以用 map 字典将 A 语言与 B 语言的每组对应单词建立索引,可参考 OI Wiki。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e4 + 5;
int n;
map<string, string> m;
string s, ans;
signed main() {
cin>>n;
for(int i = 1; i <= n; i++) {
string a, b;
cin>>a>>b;
m[a] = b;
}
cin>>s;
s += '.';
string t = "";
for(char c : s) {
if(c >= 'a' && c <= 'z') t += c;
else {
if(t != "") {
if(m.count(t)) ans += m[t];
else ans += "UNK";
t = "";
}
ans += c;
}
// cout<<c<<" "<<ans<<" "<<t<<endl;
}
ans.pop_back();
cout<<ans<<endl;
return 0;
}