# "蔚来杯"2022牛客暑期多校训练营9 G Magic Spells【马拉车+哈希】

2022年11月23日
0 收藏 917 点赞 2,918 浏览 2401 个字

hash求子串可以O(1)，

\$click\$ \$for\$ \$codes\$
``# include "bits/stdc++.h"using namespace std;constexpr int N = 3e5 + 3;char str[N], str_new[N << 1];int main() {    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);    int n;    cin >> n;    unordered_map<unsigned long long, int> mp;    unordered_map<unsigned long long, int> mp2;    unordered_map<unsigned long long, bool> vis[6];    unordered_map<unsigned long long, bool> vis2[6];    int ans = 0;    str_new[0] = '!';    str_new[1] = '@';    for(int i = 1; i <= n; ++i) {        cin >> str + 1;        int len = strlen(str + 1);        int len_new = 1;        for(int j = 1; j <= len; ++j) {            str_new[++len_new] = str[j];            str_new[++len_new] = '@';        }        str_new[++len_new] = '#';        int r = 0, center;        vector<int> p(len_new + 1 << 1);        vector<int> pos;        for(int j = 1; j <= len_new; ++j) {            p[j] = (j < r) ? min(p[(center << 1) - j], r - j) : 1;            while(str_new[j - p[j]] == str_new[j + p[j]]) ++p[j];            if(r < j + p[j]) {                r = j + p[j];                center = j;            }            if(p[j] > 1) pos.push_back(j);        }        constexpr unsigned long long base = 122777, mod = 91815541, mod2 = 19260817; // hash value, which is indescibable        vector<unsigned long long> h(len + 1), b(len + 1), h2(len + 1), b2(len + 1);        b[0] = 1, b2[0] = 1;        for(int i = 1; i <= len; ++i) {        h[i] = (h[i - 1] * base % mod + (unsigned long long) str[i]) % mod;        h2[i] = (h2[i - 1] * base % mod2 + (unsigned long long) str[i]) % mod2;        b[i] = b[i - 1] * base % mod;        b2[i] = b2[i - 1] * base % mod2;}auto hash = [&](int l, int r) -> unsigned long long {return (h[r] - h[l - 1] * (unsigned long long)b[r - l + 1] % mod + mod) % mod;};auto hash2 = [&](int l, int r) -> unsigned long long { // double hash reduces chance and thus improves accuracyreturn (h2[r] - h2[l - 1] * (unsigned long long)b2[r - l + 1] % mod2 + mod2) % mod2;};        for(auto j : pos) {            int len = p[j];            while(len) {            if((j - len + 2 >> 1) > (j + len - 2 >> 1)) break; // from several manual simulations, we'll find that the palindrome string centered on i in the new string is (i - p[i】 + 2) / 2 ---> (i + p[i] - 2) / 2 in the corresponding position in the original string                unsigned long long sub_str_1 = hash( (j - len + 2 >> 1), (j + len - 2 >> 1) );                unsigned long long sub_str_2 = hash2( (j - len + 2 >> 1), (j + len - 2 >> 1) );                if(vis[i][sub_str_1] == true && vis2[i][sub_str_2] == true) break;                vis[i][sub_str_1] = vis2[i][sub_str_2] = true;                ++mp[sub_str_1], ++mp2[sub_str_2];                if(mp[sub_str_1] >= n && mp2[sub_str_2] >= n) ++ ans;                len -= 2; // manachar only directly finds the longest palindrome string, but the string may contain other substrings                if(len < 0) break;            }        }    }    cout << ans;    return 0;}/*9999abcbaabcccba*/``

python开发_常用的python模块及安装方法

Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接：http://www.codeforces.com/contest/660/problem/CDes…

zengkefu@server1:/usr/src\$ uname -aLinux server1 4.10.0-19-generic #21…

Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式，并且由于涉及到要把拍到的照片显…

Struts的使用