hdu 3518 Boring counting 后缀数组 height分组

题目链接

题意

对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\).

思路

枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分。

显然,对于每一组,组内子串具有一个长度为 \(len\) 的公共前缀。

至于是否重叠,只需判断 \(sa_{max}-sa_{min}\geq len\).

对于组间,显然它们的公共前缀互不相同,所以答案即为\(\sum_{len}\sum_{group}\)

Code

#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
typedef long long LL;
int wa[maxn], wb[maxn], wv[maxn], wt[maxn], h[maxn], rk[maxn], sa[maxn], n, r[maxn];
char s[maxn];
char s1[maxn], s2[maxn];
bool cmp(int* r, int a, int b, int l) { return r[a] == r[b] && r[a+l] == r[b+l]; }
void init(int* r, int* sa, int n, int m) {
    int* x=wa, *y=wb, *t, i, j, p;
    for (i = 0; i < m; ++i) wt[i] = 0;
    for (i = 0; i < n; ++i) ++wt[x[i] = r[i]];
    for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
    for (i = n-1; i >= 0; --i) sa[--wt[x[i]]] = i;
    for (j = 1, p = 1; p < n; j <<= 1, m = p) {
        for (p = 0, i = n-j; i < n; ++i) y[p++] = i;
        for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
        for (i = 0; i < n; ++i) wv[i] = x[y[i]];
        for (i = 0; i < m; ++i) wt[i] = 0;
        for (i = 0; i < n; ++i) ++wt[wv[i]];
        for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
        for (i = n-1; i >= 0; --i) sa[--wt[wv[i]]] = y[i];
        t = x, x = y, y = t, x[sa[0]] = 0;
        for (p = 1, i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? p - 1 : p++;
    }

    for (i = 0; i < n; ++i) rk[sa[i]] = i;
    int k = 0;
    for (i = 0; i < n - 1; h[rk[i++]] = k) {
        for (k = k ? --k : 0, j = sa[rk[i] - 1]; r[i+k] == r[j+k]; ++k);
    }
}
void work() {
    int tot=0, m=0;
    for (int i = 0; s[i]; ++i) m = max(r[tot++] = s[i], m);
    r[tot++] = 0;
    init(r, sa, tot, ++m);
    int ans = 0;
    for (int len = 1; len < (tot+1)/2; ++len) {
        int minn = maxn, maxx = 0;
        for (int i = 1; i < tot; ++i) {
            if (h[i] < len) {
                ans += maxx-minn >= len ? 1 : 0;
                minn = sa[i], maxx = sa[i];
            }
            else minn = min(minn, sa[i]), maxx = max(maxx, sa[i]);
        }
        ans += maxx-minn >= len ? 1 : 0;
    }
    printf("%d\n", ans);
}
int main() {
    while (scanf("%s", s) != EOF && s[0] != '#') work();
    return 0;
}

原文地址:https://www.cnblogs.com/kkkkahlua/p/8439945.html

时间: 2024-10-10 09:02:01

hdu 3518 Boring counting 后缀数组 height分组的相关文章

hdu 3518 Boring counting 后缀数组LCP

题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output 2 3 3 思路:套用后缀数组求解出sa数组和height数组,之后枚举后缀的公共前缀长度i,由于不能重叠,所以计数的是相邻height不满足LCP >= i的. 写写对后缀数组倍增算法的理解: 1.如果要sa数组对应的值也是1~n就需要在最后加上一个最小的且不出现的字符'#',里面y[]是利用sa数

hdu 3518 Boring counting(后缀数组)

Boring counting                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description 035 now faced a tough problem,his english teacher gives him

后缀数组 --- HDU 3518 Boring counting

Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,让你求出有多少个子串(无重叠)至少出现了两次. analyse: 后缀数组中height数组的运用,一般这个数组用得很少. 总体思路:分组统计的思想:将相同前缀的后缀分在一个组,然后对于1到len/2的每一个固定长度进行统计ans. 首先我们先求一遍后缀数组,并把height数组求出来.height数组代表的

HDU 3518 Boring counting(后缀数组啊 求字符串中不重叠的重复出现至少两次的子串的个数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 Problem Description 035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover

POJ - 1743 后缀数组 height分组

题意:求最长不可重叠的相同差值子串的长度 这道题算是拖了好几个月,现在花了点时间应该搞懂了不少,尝试分析一下 我们首先来解决一个退化的版本,求最长不可重叠的相同子串(差值为0) 比如\(aabaabaa\), 那么所求的子串有\(aab,aba,baa\)三个 如何求?不妨枚举.枚举是否有长度为\(k\)的最长不可重叠相同子串 可是后缀数组中并不能直接表示出子串,只能间接地用后缀来表示 长度为\(k\)的相同子串\(=>\)最大公共前缀长度为\(k\)的子串\(=>\)最大公共前缀长度大于等于

[HDU]3518——Boring counting

zhan.jiang.ou now faced a tough problem,his english teacher quan.hong.chun gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.Tak

acdream 1430 SETI 后缀数组+height分组

这题昨天比赛的时候逗了,后缀想不出来,因为n^2的T了,就没往后缀数组想--而且之后解题的人又说用二分套二分来做,然后就更不会了-- 刚才看了题解,唉--原来题解说n^2的也可以过,然后就--这样了! #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algo

HDOJ 题目3518 Boring counting(后缀数组,求不重叠重复次数最少为2的子串种类数)

Boring counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2253    Accepted Submission(s): 924 Problem Description 035 now faced a tough problem,his english teacher gives him a string,whic

hdu_3518_Boring counting(后缀数组)

题目链接:hdu_3518_Boring counting 题意: 给你一个字符串,让你找不重叠且出现大于1次以上的字串个数 题解: 后缀数组height数组的应用,我们枚举字串的长度,然后将height数组分段,符合条件就ans++ 为什么要这样做,因为height数组存的是相邻排名后缀的最大前缀数,如果height的值大于等于我们枚举的长度len, 那么有可能这一段存在有两个以上的该长度的字串,然后我们统计这个段的开头长度l和结束长度r,如果r-l>=len,说明这段必有 大于一个以上的该长