题解 P3752 【语言普查】
题目链接
本题有两个坑点:
- 出现\ 、-或 ' 视为一个单词,实际上是 ' 或 - 视为单词的构成字符,其它字符作为分隔符。
- 下一个N行依次包含该语言的名称,后跟该语言中的一个或多个单词,词和词之间用空格隔开。实际上分割单词的不仅仅是空格,也有别的符号,所以如果使用getline作为读入流会Wrong,应该用题目所说的那种分割方式来读单词。
参考代码:
#include <bits/stdc++.h>
using namespace std;
bool check(char c) {
return !islower(c) && !isupper(c) && c != '\'' && c != '-';
}
void convert(string &s) {
for (auto &c : s)
if (isupper(c))
c += 'a' - 'A';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
map <string, string> mp;
string s, a, b;
getline(cin, s);
stringstream ss(s);
int n;
ss >> n;
while (n--) {
getline(cin, s);
s += ' ';
a = "";
int flag = 0;
for (auto &c : s) {
if (check(c)) {
if (flag++ && b.size())
convert(b), mp[b] = a;
b = "";
} else {
if (flag)
b += c;
else
a += c;
}
}
}
getline(cin, s);
while (getline(cin, s)) {
s += ' ';
a = "";
for (auto &c: s) {
if (check(c)) {
convert(a);
if (mp.count(a)) b = mp[a];
a = "";
} else {
a += c;
}
}
cout << b << '\n';
}
return 0;
}