L2-050 懂蛇语(测试点1)
目录
题目链接
思路
坑点
参考代码
题目链接
L2-050 懂蛇语 - 团体程序设计天梯赛-练习集
思路
创建一个map
- 键:首字母组合的字符串
- 值:对应的字符串数组
坑点
- 测试点1:首字母就是空格
参考代码
#include <bits/stdc++.h>
#define debug(x) cout << endl << "===>" << #x << "=" << x << endl
#define output(x) cout << x << endl
#define int long long
using namespace std;
void solve() {
int n;
cin >> n;
vector<string> v(n);//存n个字符串
cin.ignore();//清除输入流,非常重要
for(int i = 0; i < n; i++) {
getline(cin, v[i]);
}
unordered_map<string, vector<string>> mp;//{首字母组合,对应的结果}
for(int i = 0; i < n; i++) {
string t = "";
int len = v[i].length(), j = 0;
//遍历输入的字符串
while(j < len && v[i][j] == ' ') j++;//去掉前置空格
while(j < len) {
t.push_back(v[i][j]);//加入首字母
while(j < len && v[i][j] != ' ') j++;
while(j < len && v[i][j] == ' ') j++;
}
mp[t].push_back(v[i]);//存map
}
//按照字母序排序
for(auto & pr : mp) {
vector<string> &t = pr.second;
sort(t.begin(), t.end());
}
int m;
cin >> m;
cin.ignore();//清除输入流,非常重要
while(m--) {
string s;
getline(cin, s);
string t = "";
int len = s.length(), j = 0;
while(j < len && s[j] == ' ') j++;
while(j < len) {
t.push_back(s[j]);
while(j < len && s[j] != ' ') j++;
while(j < len && s[j] == ' ') j++;
}
//找不到
if(!mp.count(t)) {
output(s);
continue;//判断下一个
}
//找得到
vector<string> tt = mp[t];
for(int i = 0; i < tt.size(); i++) {
if(i) cout << "|";//除了第一个不要竖线
cout << tt[i];
}
cout << endl;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}