QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621056#7748. Karshilov's Matching Problem IIRosmontispesWA 205ms25380kbC++203.0kb2024-10-08 01:48:432024-10-08 01:48:44

Judging History

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

  • [2024-10-08 01:48:44]
  • 评测
  • 测评结果:WA
  • 用时:205ms
  • 内存:25380kb
  • [2024-10-08 01:48:43]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 150000 + 200;
std::vector<int> zFunction(std::string &s) {
    int n = s.size();
    std::vector<int> z(n);
    z[0] = n;
    for (int i = 1, j = 1; i < n; i++) {
        z[i] = std::max(0, std::min(j + z[j] - i, z[i - j]));
        while (i + z[i] < n && s[z[i]] == s[i + z[i]])
            z[i]++;
        if (i + z[i] > j + z[j]) {
            j = i;
        }
    }
    return z;
};
std::vector<int> EXKMP(string &s,string &t)
{
    string tmp = s + '#' + t;
    return zFunction(tmp);
}
int st[N][20];
void solve(){
    int n,m;
    cin>>n>>m;
    string s,t;
    cin>>s>>t;
    auto z = EXKMP(s,t);
    z[0] = n;
    vector<int>z1(n),z2(n);
    vector<long long>W(n),w(n),pre(n + 1),p(n + 1),iz(n),P(n + 1);
    for(int i = 0;i < n;i ++){
        z1[i] = z[i],z2[i] = z[i + n + 1];
    }
    for(int i = 0;i < n;i ++){
        cin>>w[i];W[i] = w[i];
    }
    for(int i = 1;i < n;i ++)
        W[i] += W[i - 1];
    for(int i = 1;i <= n;i ++){
        if(z2[i - 1] == 0)
            pre[i] = pre[i - 1];
        else
            pre[i] = pre[i - 1] + W[z2[i - 1] - 1];
    }
    vector<int>fail(n + 1);
    fail[0] = 0;
    P[0] = w[0];
    for(int i = 1;i < n;i ++){
        int j = fail[i - 1];
        while(j != 0 && s[i] != s[j])
            j = fail[j - 1];
        fail[i] = j + (s[i] == s[j]);
        if(fail[i] > 0)
            P[i] = P[fail[i] - 1] + w[i];
        else
            P[i] = w[i];
    }
    for(int i = 1;i <= n;i ++)
    {
        P[i] += P[i - 1];
    }
    for(int i = 0;i < n;i ++){
        if(z2[i] > 0)
            iz[i] = i + z2[i] - 1;
        else
            iz[i] = -1;
    }
    for(int i = 0;i < n;i ++)
        st[i][0] = iz[i];
    for(int j = 1;j <= 19;j ++)
        for(int i = 0;i < n;i ++){
            if(i + (1<<(j - 1)) >= n){
                st[i][j] = st[i][j - 1];
                continue;
            }
            st[i][j] = max(st[i][j - 1],st[i + (1<<(j - 1))][j - 1]);
        }
    auto get = [&](int l,int r)->int
    {
        int log = floor(log2(r - l + 1));
        return max(st[l][log],st[r - (1<<log) + 1][log]);   
    };
    for(int i = 1;i <= m;i ++){
        int L,R;
        cin>>L>>R;
        L --,R --;
        if(get(L,R) <= R){
            cout<<pre[R + 1] - pre[L]<<"\n";
        } else {
            if(L + z2[L] - 1 > R){
                cout<<P[R - L]<<"\n";
                continue;
            }
            int l = L,r = R;
            while(l + 1 < r){
                int mid = (l + r) / 2;
                if(get(L,mid) <= R)
                    l = mid;
                else
                    r = mid;
            }
            if(get(L,r) <= R){
                cout<<pre[r] - pre[L] + P[R - r - 1]<<"\n";
            } else {
                cout<<pre[l] - pre[L] + P[R - l - 1]<<"\n";
            }
            
            
        }
    }

    
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 3608kb

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
Wrong Answer
time: 205ms
memory: 25380kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

108147037823514
221872189193426
455333411674094
440279802369273
148109351852689
88695249853257
351159618462315
58850354020997
65818038768457
270158033672354
197732558443069
298187498639505
239511187032650
28132758177777
408373775781679
268047034883854
32410725132417
104807152175127
44068530005071
78...

result:

wrong answer 2nd lines differ - expected: '221878585246974', found: '221872189193426'