You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
//总结本题使用unordered_map+vector的方法为每个首次进入的字符串分配一个号码使用unordered_map来保存字符串
//和号码的对应关系然后把这个号码作为二维数组的一维数组下标成功实现了字符串的Hash和查找。
int n; //短文篇数
int l; //表示这篇短文由 LL 个单词组成
int m; //做几次询问
int cnt; //字符串映射的整数值
vector<int> linker[100010];//Hash表
unordered_map<string, int> _map;
//字符串
string s;
int main() {
//读入
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &l);
for (int j = 1; j <= l; j++) {
cin >> s;
//存入Hash表
if (!_map[s]) {
cnt++; //为s获得到一个对应的数字cnt
_map[s] = cnt;//建立起映射关系
//链表记录
linker[cnt].push_back(i);
} else {
//给linker[_map[s]]起一个别名v,下面的代码可以简单一些,注意,这可不是拷贝出来!!
vector<int> &v = linker[_map[s]];
v.push_back(i);
//去重
v.erase(unique(v.begin(), v.end()), v.end());
}
}
}
//询问
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> s;
vector<int> &v = linker[_map[s]];
for (int j = 0; j < v.size(); j++)
printf("%d ", v[j]);
//换行
cout << endl;
}
return 0;
}