hdu 3518 Boring counting 后缀数组基础题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2549    Accepted Submission(s): 1030


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,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
 

 

Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
 

 

Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
 

 

Sample Input
aaaa
ababcabb
aaaaaa
#
 

 

Sample Output
2
3
3
 

 

Source
 

思路:熟悉heigh数组就好了。枚举子串的长度k,将heigh数组按顺序分组,若该组的heigh值都大于等于k,则具有长度为k的公共前缀。比如heigh:3,2,3,1,2,0,1  当k=2时,分成(3,2,3) (1) (2) (0,1)

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int x = 123;
const int maxn = 1010;
int t1[maxn], t2[maxn], c[maxn];
bool cmp(int *r, int a, int b, int l) {
    return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(char str[], int sa[], int Rank[], int heigh[], int n, int m)
{
    n++;
    int i, j, p, *x = t1, *y = t2;
    for(i = 0; i < m; ++i) c[i] = 0;
    for(i = 0; i < n; ++i) c[ x[i] = str[i] ]++;
    for(int i = 1; i < m; ++i) c[i] += c[i - 1];
    for(int i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i;

    for(int j = 1; j <= n; j <<= 1)
    {
        p = 0;
        for(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 < m; ++i) c[i] = 0;
        for(i = 0; i < n; ++i) c[x[y[i]]]++;
        for(i = 1; i < m; ++i) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[ sa[0] ] = 0;
        for(i = 1; i < n; ++i)
            x[ sa[i] ] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
    int k = 0;
    n--;
    for(i = 0; i <= n; ++i) Rank[ sa[i] ] = i;
    for(i = 0; i < n; ++i) {
        if(k) k--;
        j = sa[Rank[i] - 1];
        while(str[i + k] == str[j + k]) k++;
        heigh[ Rank[i] ] = k;
    }
}

int Rank[maxn], heigh[maxn], sa[maxn];
char s[maxn];
void out(int n) {
    ///Rank数组的有效范围是0~n-1, 值是1~n
    for(int i = 0; i <= n; ++i) printf("%d ", Rank[i]);
    puts("");
    ///sa数组的有效范围是1~n,值是0~n-1
    for(int i = 0; i <= n; ++i) printf("%d ", sa[i]);
    puts("");
    ///heigh数组的有效范围是2~n
    for(int i = 0; i <= n; ++i) printf("%d ", heigh[i]);
}
int calc(int k, int n) {
    int mi = INF, mx = -INF, res = 0;
    for(int i = 2; i <= n; ++i) {
        if(heigh[i] >= k) {
            mi = min(mi, min(sa[i - 1], sa[i]));
            mx = max(mx, max(sa[i - 1], sa[i]));
        }else {
            if(mx - mi >= k) res++;
            mx = -INF, mi = INF;
        }
    }
    if(mi != INF && mx - mi >= k) res++;
    return res;
}
int main()
{
   // freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
    while(scanf("%s", s)) {
        if(strcmp(s, "#") == 0) break;
        int n = strlen(s);
        da(s, sa, Rank, heigh, n, 130);
       // out(n);
        int ans = 0;
        for(int i = 1; i <= (n >> 1); ++i) ans += calc(i, n);
        printf("%d\n", ans);
    }
    return 0;
}
View Code

 

 

一开始的做法是hash+map,n^2logn会tle? 不明觉厉,暂时保存一下,忘指点。。。

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int x = 123;
const int maxn = 1010;
ull H[maxn], xp[maxn];
map<ull, pair<int, int> > m;
int len;
char s[maxn];
void init() {
    xp[0] = 1;
    for(int i = 1; i <= 1005; ++i) xp[i] = xp[i - 1] * x;
}
int main() {
   // freopen("in.txt", "r", stdin);
   // freopen("out1.txt", "w", stdout);
    init();
    while(scanf("%s", s)) {
        if(strcmp(s, "#") == 0) break;
        len = strlen(s);
        H[len] = 0;
        for(int i = len - 1; i >= 0; --i) H[i] = H[i + 1] * x + (s[i]);
        m.clear();
        long long ans = 0;
        for(int k = 1; k <= len / 2; ++k) {
            for(int i = 0; i <= len - k; i++) {
                ull hash = H[i] - H[i + k] * xp[k];
                int lp = m[hash].first;
                int is = m[hash].second;
                if(is == 1 && i >= lp + k) {
                    m[hash].second = -1;
                    ans++;
                }else if(is == -1) {
                    continue;
                }else if(is == 0) {
                    m[hash].first = i;
                    m[hash].second = 1;
                }
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
View Code

 

poj1743

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int x = 123;
const int maxn = 2000000 + 10;
int t1[maxn], t2[maxn], c[maxn];
bool cmp(int *r, int a, int b, int l) {
    return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int str[], int sa[], int Rank[], int heigh[], int n, int m)
{
    n++;
    int i, j, p, *x = t1, *y = t2;
    for(i = 0; i < m; ++i) c[i] = 0;
    for(i = 0; i < n; ++i) c[ x[i] = str[i] ]++;
    for(int i = 1; i < m; ++i) c[i] += c[i - 1];
    for(int i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i;

    for(int j = 1; j <= n; j <<= 1)
    {
        p = 0;
        for(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 < m; ++i) c[i] = 0;
        for(i = 0; i < n; ++i) c[x[y[i]]]++;
        for(i = 1; i < m; ++i) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[ sa[0] ] = 0;
        for(i = 1; i < n; ++i)
            x[ sa[i] ] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
    int k = 0;
    n--;
    for(i = 0; i <= n; ++i) Rank[ sa[i] ] = i;
    for(i = 0; i < n; ++i) {
        if(k) k--;
        j = sa[Rank[i] - 1];
        while(str[i + k] == str[j + k]) k++;
        heigh[ Rank[i] ] = k;
    }
}

int Rank[maxn], heigh[maxn], sa[maxn];
int s[maxn];
void out(int n) {
    ///Rank数组的有效范围是0~n-1, 值是1~n
    for(int i = 0; i <= n; ++i) printf("%d ", Rank[i]);
    puts("");
    ///sa数组的有效范围是1~n,值是0~n-1
    for(int i = 0; i <= n; ++i) printf("%d ", sa[i]);
    puts("");
    ///heigh数组的有效范围是2~n
    for(int i = 0; i <= n; ++i) printf("%d ", heigh[i]);
}
int flag, ans;
bool check(int k, int n) {
    flag = 0;
    int mi = INF, mx = -INF;
    for(int i = 2; i <= n; ++i) {
        if(heigh[i] >= k) {
            mi = min(mi, min(sa[i - 1], sa[i]));
            mx = max(mx, max(sa[i - 1], sa[i]));
        }else {
            if(mx - mi >= k + 1) return true;
            mx = -INF, mi = INF;
        }
    }
    if(mi != INF && mx - mi >= k + 1) return true;
    return false;
}
void solve(int n) {
    int L = 0, R = n + 1;
    while(R - L > 1) {
        int M = (L + R) >> 1;
        if(check(M, n)) L = M;
        else R = M;
    }
    ans = L;
}
int main() {
    int n;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; ++i) scanf("%d", &s[i]);
        for(int i = 0; i < n - 1; ++i) {
                s[i] = s[i + 1] - s[i];
                s[i] += 90;
        }
        //for(int i = 0; i < n - 1; i++) printf("%d ", s[i]);
        n--;
        s[n] = 0;
        da(s, sa, Rank, heigh, n, 200);
            solve(n);
            if(ans + 1 < 5) puts("0");
            else
            printf("%d\n", ans + 1);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/orchidzjl/p/5079873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值