日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Singing Superstar 字符串哈希-map操作

發(fā)布時(shí)間:2025/3/19 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Singing Superstar 字符串哈希-map操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意 :

  • 給一長(zhǎng)度 < 1e5 的字符串s,q < 1e5次詢問(wèn),每次問(wèn)一個(gè)長(zhǎng) < 30 的串 t 在s中出現(xiàn)的次數(shù),且t不可重疊。
  • 例 : “abababa“中“aba“不相交的出現(xiàn)的次數(shù)為2。

思路 :

  • 字符串題,對(duì)母串中所有長(zhǎng)度小于等于30的串做字符串哈希,對(duì)相同哈希值的串暴力統(tǒng)計(jì)答案,每個(gè)詢問(wèn)直接查詢對(duì)應(yīng)哈希值的答案。
  • 也可以使用Trie/AC自動(dòng)機(jī)來(lái)通過(guò)本題。
#include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <map> #define endl '\n' #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; const double pi = acos(-1); typedef long long ll;typedef unsigned long long ull;const int N = 1e5 + 10, P = 131;ull h[N], p[N]; ull a[N]; // 記錄每個(gè)詢問(wèn)串的哈希值 char str[N];ull get(int l, int r) {return h[r] - h[l - 1] * p[r - l + 1]; }int main() {int T;scanf("%d", &T);while (T -- ){scanf("%s", str + 1);int len = strlen(str + 1);p[0] = 1;for (int i = 1; i <= len; i ++ ){p[i] = p[i - 1] * P;h[i] = h[i - 1] * P + str[i];}int n;scanf("%d", &n);map<ull, int> ma; // 詢問(wèn)串哈希值在母串中出現(xiàn)的不相交的次數(shù)map<ull, int> last; // 詢問(wèn)串哈希值在母串中上次出現(xiàn)的位置的末端for (int i = 1; i <= n; i ++ ){char buf[35];scanf("%s", buf + 1);int size = strlen(buf + 1);// 處理詢問(wèn)串哈希值ull x = 0;for (int j = 1; j <= size; j ++ )x = x * P + buf[j];a[i] = x;ma[a[i]] = 0;last[a[i]] = 0;}// 暴力枚舉母串中長(zhǎng)度小于等于30的串的哈希值for (int i = 1; i <= len; i ++ ){for (int j = 0; j < 30 && i + j <= len; j ++ ){ull x = get(i, i + j);// []包含了插入,count不插入,count查詢是lognif (ma.count(x) && last[x] < i){ma[x] ++ ;last[x] = i + j;}}}for (int i = 1; i <= n; i ++ )printf("%d\n", ma[a[i]]);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Singing Superstar 字符串哈希-map操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。