RE求助
查看原帖
RE求助
678858
ShiRoZeTsuHL卜奎BBQ!楼主2023/9/17 19:05

并不知道是哪里出问题了,求助

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 2e3 + 5;

int t, n, m, ans;
int cnt[257], sa[maxn], tmp[maxn], rk[maxn], height[maxn];
char s[maxn];

void getsa() {
    for(int i = 0; i <= m; i++) cnt[i] = 0;
    for(int i = 1; i <= n; i++) cnt[rk[i] = s[i]]++;
    for(int i = 1; i <= m; i++) cnt[i] += cnt[i-1];
    for(int i = n; i >= 1; i--) sa[cnt[rk[i]]--] = i;
    for(int w = 1, t = 0; ; m = t, t = 0, w <<= 1) {
        for(int i = n-w+1; i <= n; i++) tmp[++t] = i;
        for(int i = 1; i <= n; i++)
            if(sa[i] > w) tmp[++t] = sa[i] - w;
        for(int i = 0; i <= m; i++) cnt[i] = 0;
        for(int i = 1; i <= n; i++) cnt[rk[tmp[i]]]++;
        for(int i = 1; i <= m; i++) cnt[i] += cnt[i-1];
        for(int i = n; i >= 1; i--) sa[cnt[rk[tmp[i]]]--] = tmp[i];
        swap(rk, tmp); t = 0;
        for(int i = 1; i <= n; i++)
            rk[sa[i]] = (tmp[sa[i]] == tmp[sa[i-1]]
                    && tmp[sa[i]+w] == tmp[sa[i-1]+w] ? t : ++t);
        if(t == n) break;
    }
}

void getheight() {
    for(int i = 1, p = 0; i <= n; i++) {
        if(!rk[i]) continue;
        if(p) p--;
        while(s[i+p] == s[sa[rk[i]-1]+p]) p++;
        height[rk[i]] = p;
    }
}

int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%s", s+1);
        n = strlen(s+1), m = 256;
        getsa();
        getheight();
        ans = ((n+1)*n) >> 1;
        for(int i = 1; i <= n; i++)
            ans -= height[i];
        printf("%d\n", ans);
    }
    return 0;
}
2023/9/17 19:05
加载中...