QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#461978#8475. Palindrome Stringsucup-team052#WA 0ms18036kbC++143.1kb2024-07-03 11:33:332024-07-03 11:33:34

Judging History

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

  • [2024-07-03 11:33:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:18036kb
  • [2024-07-03 11:33:33]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;

template <typename _T>
inline void read(_T &f) {
    f = 0; _T fu = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
    while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
    f *= fu;
}

template <typename T>
void print(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x < 10) putchar(x + 48);
    else print(x / 10), putchar(x % 10 + 48);
}

template <typename T>
void print(T x, char t) {
    print(x); putchar(t);
}

const int N = 2e6 + 5;

char s[N], ss[N], c[N];
int val[N], dis[N], mxr, mid;
int n, m, q;

ll sum[N];
int ch[N][26], fa[N], len[N], id[N], siz[N];
int cnt = 1, las = 1;

void extend(int x, int v) {
    int p = las, np = las = ++cnt; len[np] = len[p] + 1;
    for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
    if (!p) fa[np] = 1;
    else {
        int q = ch[p][x];
        if (len[q] == len[p] + 1) fa[np] = q;
        else {
            int nq = ++cnt; len[nq] = len[p] + 1;
            memcpy(ch[nq], ch[q], sizeof(ch[q]));
            fa[nq] = fa[q]; fa[q] = fa[np] = nq;
            for (; ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
        }
    }
    sum[np] = v; siz[np] = 1;
}

bool cmp(int i, int j) {
    return len[i] > len[j];
}

void manacher(char *s, int n) {
    mxr = mid = m = 0;
    ss[0] = '#'; ss[++m] = '?';
    for (int i = 1; i <= n; i++) {
        ss[++m] = s[i];
        ss[++m] = '?';
    }
    ss[m + 1] = '!';
    for (int i = 1; i <= m; i++) {
        if (i <= mxr) dis[i] = min(mxr - i, dis[mid * 2 - i]);
        while (ss[i - dis[i] - 1] == ss[i + dis[i] + 1]) ++dis[i];
        if (i + dis[i] > mxr) mxr = i + dis[i], mid = i;
    }
}

int main() {
    read(n); read(q);
    scanf("%s", s + 1);
    manacher(s, n);
    for (int i = 1; i <= m; i++) {
        if (dis[i]) {
            if (i & 1) ++val[i / 2 + 1], --val[i / 2 + 1 + dis[i] / 2];
            else ++val[i / 2], --val[i / 2 + 1 + dis[i] / 2];
        }
    }
    for (int i = 1; i <= n; i++) val[i] += val[i - 1];
    for (int i = n; i >= 1; i--) extend(s[i] - 'a', val[i - 1]);
    for (int i = 1; i <= cnt; i++) id[i] = i;
    sort(id + 1, id + cnt + 1, cmp);
    for (int i = 1; i <= cnt; i++) {
        int u = id[i];
        sum[fa[u]] += sum[u];
        siz[fa[u]] += siz[u];
    }
    while (q--) {
        scanf("%s", c + 1);
        int len = strlen(c + 1);
        manacher(c, len);
        ll ans = 0;
        int now = 1;
        for (int i = 1; i <= len; i++) {
            now = ch[now][c[i] - 'a'];
            if (!now) break;
            // check [i + 1, len]
            int ok = 0;
            if (i == len) ok = 1;
            else if (dis[i + 1 + len] == len - i) ok = 1;
            if (ok) ans += siz[now];
        }
        if (now) ans += sum[now];
        print(ans, '\n');
    }
    return 0;
}

详细

Test #1:

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

input:

8 3
icpccamp
p
c
pc

output:

4
7
4

result:

ok 3 number(s): "4 7 4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 17956kb

input:

10 3
bbbabbbbbb
baaaa
abb
bb

output:

10
4
31

result:

ok 3 number(s): "10 4 31"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 18036kb

input:

10 4
baababaaba
aaaaa
a
ab
aa

output:

8
18
11
11

result:

wrong answer 3rd numbers differ - expected: '17', found: '11'