#include <iostream>
#include <vector>
#include <string>
#define mod 11451
#define base 26
#define endl "\n"
#define ts ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
vector<string> st[1001][mod];
long long strhash(string a)
{
long long gthash = 1;
for (int j = 0; j < a.size(); j++)
{
gthash = (gthash % mod * base % mod + int(a[j])) % mod;
}
return gthash;
}
void add(int n, string a)
{
long long gthash = strhash(a);
for (int i = 0; i < st[n][gthash].size(); i++)
{
if (st[n][gthash][i] == a)
{
return;
}
}
st[n][gthash].push_back(a);
}
void fd(int n, string a)
{
long long gthash = strhash(a);
for (int i = 0; i < st[n][gthash].size(); i++)
{
if (st[n][gthash][i] == a)
{
cout << n << ' ';
break;
}
}
}
int main()
{
ts;
int n, m, l;
string a;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> l;
while (l--)
{
cin >> a;
add(i, a);
}
}
cin >> m;
while (m--)
{
cin >> a;
for (int i = 1; i <= n; i++)
{
fd(i, a);
}
cout << endl;
}
}
TLE了,向各位大佬求助有没有优化方案? 提交记录