算法.字符串哈希表

#include
#include
#include
using namespace std;
const int N = 100010, P = 131;
int n, m;
typedef unsigned long long ULL;
ULL p[N], h[N];
char str[N];
 ULL get(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
    cin >> n >> m;
    cin >> str + 1;
    for (int i = 1; i <= n; i++)
    {
        h[i] = h[i - 1] * P + str[i];
        p[i] = p[i - 1] * P;
    }
    while (m--)
    {
        int l1, l2, r1, r2;
        if (get(l1, r1) == get(l2, r2))
        {
            cout << "Yes";
        }
        else cout << "No";
    }

    return 0;
}

你可能感兴趣的:(算法,散列表,数据结构)