社区讨论

Hash算法90分求调

P3879[TJOI2010] 阅读理解参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m38kc6r7
此快照首次捕获于
2024/11/08 17:57
去年
此快照最后确认于
2025/11/04 15:07
4 个月前
查看原帖
C
#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了,向各位大佬求助有没有优化方案? 提交记录

回复

2 条回复,欢迎继续交流。

正在加载回复...