QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#282531#7748. Karshilov's Matching Problem IIHYX1124RE 1ms9856kbC++143.2kb2023-12-12 11:28:162023-12-12 11:28:17

Judging History

你现在查看的是最新测评结果

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-12-12 11:28:17]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:9856kb
  • [2023-12-12 11:28:16]
  • 提交

answer

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "lib.h"
#endif
#define rep(i, min, max) for(int i = (min); i <= (max); ++i)
#define nrep(i, max, min) for(int i = (max); i >= (min); --i)
#define reads(str) (scanf("%s", str + 1), strlen(str + 1))
#define case() int Ts = read(); rep(T, 1, Ts)
#define putf(flag) puts((flag) ? "YES" : "NO")
#define put(x) printf("%d ", x)
#define putl(x) printf("%lld ", x)
#define endl() putchar('\n')
using namespace std;

typedef long long ll;
inline int read()
{
    int now=0; bool nev=false; char c=getchar();
    while(c<'0' || c>'9') { if(c=='-') nev=true; c=getchar(); }
    while(c>='0' && c<='9') { now=(now<<1)+(now<<3)+(c&15); c=getchar(); }
    return nev?-now:now;
}

const int N = 1e5 + 10;

char S[N], T[N];
ll w[N];
int n;

namespace AC {
    int fail[N], fa[N], tr[N][26];
    ll val[N], sum[N];
    int alloc;
    // struct Val {
    //     int from, step;
    // } f[N][20];
    
    void insert() {
        int x = 0;
        rep(o, 1, n) {
            int i = S[o] - 'a';
            if(!tr[x][i]) tr[x][i] = ++alloc;
            x = tr[x][i];
            val[x] += w[o];
        }
    }
    void build() {
        queue<pair<int, int>> q;
        rep(i, 0, 25) if(tr[0][i]) q.push({tr[0][i], 0});
        while(!q.empty()) {
            int x = q.front().first;
            fa[x] = q.front().second; q.pop();
            rep(i, 0, 25) {
                if(tr[x][i]) fail[tr[x][i]] = tr[fail[x]][i],
                    q.push({tr[x][i], x});
                else tr[x][i] = tr[fail[x]][i];
            }
            val[x] += val[fail[x]];
            sum[x] = sum[fa[x]] + val[x];
        }
        // parray(val, n + 1, 0);
    }
    unordered_map<ll, int> id;
    inline ll _(ll x, ll y) {
        return x * N * 2 + y;
    }
    int idtop;
    int nxt[N << 3][20]; 
    ll f[N << 3][20]; 
    void solve() {
        nrep(i, n, 1) {
            int x = 0, last = (id[_(0, i - 1)] = ++idtop);
            vector<int> vec = {last};
            rep(j, i, n) {
                int v = T[j] - 'a';
                x = tr[x][v];
                ll cur = _(x, j);
                f[last][0] = val[x];
                if(id.count(cur)) {
                    nxt[last][0] = id[cur];
                    break;
                }
                vec.push_back(id[cur] = ++idtop);
                nxt[last][0] = idtop;
                last = idtop;
            }
            // f.resize(idtop + 1);
            rep(k, 1, 18) {
                for(int x : vec) {
                    nxt[x][k] = nxt[nxt[x][k - 1]][k - 1];
                    f[x][k] = f[x][k - 1] + f[nxt[x][k - 1]][k - 1];
                }
            }
        }
    }
    ll query(int l, int r) {
        int x = id[_(0, l - 1)], siz = r - l + 1;
        ll sum = 0;
        rep(k, 0, 18) if(siz & (1 << k)) {
            sum += f[x][k];
            x = nxt[x][k];
        }
        return sum;
    }
}

int main() {
    n = read(); int q = read();
    reads(S), reads(T);
    rep(i, 1, n) w[i] = read();
    AC :: insert();
    AC :: build();
    AC :: solve();
    while(q--) {
        int l = read(), r = read();
        putl(AC :: query(l, r)), endl();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9776kb

input:

8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8

output:

1 
3 
3 
16 
38 

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 9856kb

input:

15 4
heheheheehhejie
heheheheheheheh
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
2 3
4 8
2 6
1 15

output:

3 
13 
13 
174 

result:

ok 4 lines

Test #3:

score: -100
Runtime Error

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:


result: