QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#540490#8339. Rooted TreeOldMemoryWA 1ms4004kbC++142.7kb2024-08-31 17:09:322024-08-31 17:09:32

Judging History

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

  • [2024-08-31 17:09:32]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4004kb
  • [2024-08-31 17:09:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;
typedef long long LL;
const int INF = 0x3f3f3f3f3f3f3f3f;

bool multi = 0;

constexpr LL B = 1413214, P = 1e18 + 9;
struct strHash{
    int n;
    vector<LL> hz,pz;
    strHash() {}
    strHash(int n_, string &str) {
        init(n_, str);
    }
    constexpr LL mul(LL a, LL b, LL p = P) {
        LL res = a * b - LL(1.L * a * b / p) * p;
        res %= p;
        if (res < 0) {
            res += p;
        }
        return res;
    }
    void init(int n_ ,string &str) {
        n = n_;
        hz.resize(n + 2);
        pz.resize(n + 2);
        pz[0] = 1;
        for(int i = 1; i <= n; i++) {
            hz[i] = (mul(hz[i-1], B) + str[i]) % P;
            pz[i] = mul(pz[i-1], B);
        }
    }
    LL findz(int l,int r){
        return ((hz[r]-hz[l-1]*pz[r-l+1])%P+P)%P;
    }
};

constexpr int len = sqrt(6000);

void solve(){
    int n, q, m, k;
    cin >> n >> q >> m >> k;
    vector<string> s(n + 1);
    vector<strHash> hs(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] = ' ' + s[i];
        hs[i].init(m, s[i]);
    }
    vector<int> lb, rb;
    int curl = 1;
    while(curl <= m) {
        int curr = min(m, curl + len - 1);
        lb.push_back(curl);
        rb.push_back(curr);
        curl = curr + 1;
    }
    int tot = lb.size();
    vector<vector<int>> bhs(n + 1, vector<int>(lb.size()));
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < tot; j++) {
            bhs[i][j] = hs[i].findz(lb[j], rb[j]);
        }
    }
    while(q--) {
        string t;
        cin >> t;
        t = ' ' + t;
        strHash ht(m, t);
        vector<int> ths(lb.size());
        for(int i = 0; i < lb.size(); i++) {
            ths[i] = ht.findz(lb[i], rb[i]);
        }
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int curk = k;
            bool ok = 1;
            for(int j = 0; j < tot; j++) {
                if(ths[j] != bhs[i][j]) {
                    for(int p = lb[j]; p <= rb[j]; p++) {
                        if(t[p] != s[i][p]) {
                            curk--;
                            if(curk < 0) {
                                j = tot;
                                ok = 0;
                                break;
                            }
                        }
                    }
                }
            }
            if(ok) {
                ans++;
            }
        }
        cout << ans << '\n';
    }
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int T = 1;
    if(multi) cin >> T;
    while(T--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4004kb

input:

6 2

output:

6
6

result:

wrong answer 1st numbers differ - expected: '18', found: '6'