[NOI2017]蚯蚓排队 题解

题目就是要求我们能够支持三个操作:

1.把两个字符串合并

2.把两个字符串分裂

3.询问有多少一些个串是这么多串中出现了多少次

我们发现数据范围中的 K K K很小,于是我们就考虑出题人为什么 K K K这么小

这样的话需要匹配的字符串的长度最长就只有 K K K

于是我们发现第3中操作可以通过哈希表解决,哈希表只需要存储长度 < = K <=K <=K的子串的哈希值即可(哈希表就是挂链就行了)

我们思考,两个字符串 a , b a,b a,b合并会增加哪些哈希值? 其实就是那些即有 a a a的部分又有 b b b部分的子串。 这样的串有多少个?可能有很多,但是我们记录的最多有 K 2 K^2 K2个。

分裂?不也是一样的么,就是把那些即有 a a a的部分又有 b b b部分的子串的哈希值从哈希表中移除

对于第三种,只需要 O ( ∣ S ∣ ) O(|S|) O(S)的枚举串一遍,然后滚动哈希一下不就行了么

有应为 ∑ ∣ S ∣ < = 1 0 7 \sum{|S}|<=10^7 S<=107,所以总时间复杂度大概就是 O ( 1 0 7 + M K 2 ) = > O ( M K 2 ) O(10^7+MK^2)=>O(MK^2) O(107+MK2)=>O(MK2)

最好用读入优化,防止TLE

打代码的难度还是有的,大家加油吧

代码如下

#include 
using namespace std ;
#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define per(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define enter cout << endl
#define siz(x) ((int)x.size())
typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi ;
typedef pair <int, int> pii ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int M = 20000000 ;
const int P = 10000009 ;
const int K = 50 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 998244353 ;
const ull bs = 7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

namespace io {
    const int BUFSIZE = 1 << 20 ;
    char ibuf[BUFSIZE], *is = ibuf, *it = ibuf ;
    char readc() {
        if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin) ;
        return *is++ ;
    }
    int readi() {
        int x = 0, f = 1 ;
        char c = readc() ;
        while (!isdigit(c)) {
            if (c == '-') f = -1 ;
            c = readc() ;
        }
        while (isdigit(c)) {
            x = x * 10 + c - '0' ;
            c = readc() ;
        }
        return x * f ;
    }
}

using io::readc ;
using io::readi ;

struct Hash_table {
    int tot, head[P], nxt[M], val[M] ; ull to[M] ;
    void modify(ull x, int sum) {
        int ind = x % P ;
        for (int i = head[ind]; i; i = nxt[i]) // link
        if (to[i] == x){ // appeared
            val[i] += sum ;
            return ;
        }
        to[++tot] = x ; // first appear, push into the hash table
        nxt[tot] = head[ind] ;
        val[tot] = sum ;
        head[ind] = tot ;
    }
    int query(ull x) {
        for (int i = head[x % P]; i; i = nxt[i]) if (to[i] == x) return val[i] ; // find
        return 0 ; // not find
    }
} Hash ;

int n, m ;
int pre[N], suf[N], str[N], s[M] ;
ull pw[N] ;

signed main(){
    n = readi() ; m = readi() ;
    for (int i = 1; i <= n; i++) {
        str[i] = readi() ;
        Hash.modify(str[i], 1) ;
    }
    pw[0] = 1 ; for (int i = 1; i <= K; i++) pw[i] = pw[i - 1] * bs ;
    while (m--) {
        int ty = readi(), k ;
        if (ty == 1) {
            int x = readi(), y = readi() ; 
            ull now = 0 ;
            pre[y] = x, suf[x] = y ; // change link
            for (int i = x, p = 1; i && p <= K; i = pre[i], p++) {
                ull hqg = now += pw[p - 1] * str[i] ;
                for (int j = y, q = p + 1; j && q <= K; j = suf[j], q++) {
                    hqg = hqg * bs + str[j] ;
                    Hash.modify(hqg, 1) ;
                }
            }
        } else
        if (ty == 2) {
            int x = readi(), y ; y = suf[x] ;
            ull now = 0 ;
            pre[y] = suf[x] = 0 ;
            for (int i = x, p = 1; i && p <= K; i = pre[i], p++) {
            	ull hqg = now += pw[p - 1] * str[i] ;
            	for (int j = y, q = p + 1; j && q <= K; j = suf[j], q++) {
                    hqg = hqg * bs + str[j] ;
                    Hash.modify(hqg, -1) ;
                }
            }
        } else {
            char c = readc() ;
            while (!isdigit(c)) c = readc() ;
            int len = 0 ;
            while (isdigit(c)) s[len++] = c - '0', c = readc() ;
            k = readi() ;
            ull now = 0, ans = 1 ;
            for (int i = 0; i < len; i++) {
                now = now * bs + s[i] ;
                if (i >= k - 1) {
                    ans = ans * Hash.query(now) % MOD ;
                    now -= pw[k - 1] * s[i - k + 1] ;
                }
            }
            printf("%llu\n", ans) ;
        }
    }
    return 0 ;
}

你可能感兴趣的:(———字符串———,字符串哈希)