hdu4416 Good Article Good sentence (后缀数组)

题意:问a串中有多少种字符串集合B中没有的连续子串。

a的长度10^5,B中的总长度为不超过10^5.


解法:后缀数组题目;后缀数组能够非常easy算出来一个串中有多少种子串。

把a和B集合连起来。求一次不同子串数量,然后减掉B相互连起来的数量。

在求时候,要减掉含有链接符的子串,方法是扫一遍,枚举最后出现的连接符。


代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)  
const double pi=acos(-1.0);
typedef long long LL;
const LL INF=0x3FFFFFFF;

const int MAX=300010;
int n, num[MAX];
char s[MAX];
int sa[MAX], rank[MAX], height[MAX];//sa[i]表示排名第i的后缀的位置,height[i]表示后缀SA[i]和SA[i-1]的最长公共前缀
int wa[MAX], wb[MAX], wv[MAX], wd[MAX];
/* *suffix array *倍增算法  O(n*logn) *待排序数组长度为n,放在0~n-1中,在最后面补一个0 *da(str ,n+1,sa,rank,height,  ,   );//注意是n+1; *比如: *n   = 8; *num[]   = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其它大于0 *rank[]  = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };
rank[0~n-1]为有效值,rank[n]必然为0无效 值 *sa[]    = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值。sa[0]必然为n是无效值 *height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值 * */

int t1[MAX],t2[MAX],c[MAX];//求SA数组须要的中间变量,不须要赋值 //待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,
//除s[n-1]外的全部s[i]都大于0。r[n-1]=0 //函数结束以后结果放在sa数组中
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 n,int m)
{
    n++;
    int i, j, p, *x = t1, *y = t2;     //第一轮基数排序,假设s的最大值非常大,可改为高速排序
    for(i = 0; i < m; i++)c[i] = 0;
    for(i = 0; i < n; i++)c[x[i] = str[i]]++;
    for(i = 1; i < m; i++)c[i] += c[i-1];
    for(i = n-1; i >= 0; i--)sa[--c[x[i]]] = i;
    for(j = 1; j <= n; j <<= 1)
    {
        p = 0;         //直接利用sa数组排序第二keyword
        for(i = n-j; i < n; i++)y[p++] = i;//后面的j个数第二keyword为空的最小
        for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;         //这样数组y保存的就是依照第二keyword排序的结果
        //基数排序第一keyword
        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];        //依据sa和x数组计算新的x数组
        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++;
        height[rank[i]] = k;
    }
}

void da1(int *r, int n, int m)            //  倍增算法0(nlgn)。
{
    int i, j, p, *x = wa, *y = wb, *t;
    for(i = 0; i < m; i ++) wd[i] = 0;
    for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;
    for(i = 1; i < m; i ++) wd[i] += wd[i-1];
    for(i = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, 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 ++) wd[i] = 0;
        for(i = 0; i < n; i ++) wd[wv[i]] ++;
        for(i = 1; i < m; i ++) wd[i] += wd[i-1];
        for(i = n-1; i >= 0; i --) sa[-- wd[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++)
        {
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1: p ++;
        }
    }
}

LL get(int* p,int m,int hh)
{
    da(p,m,hh+2);
    LL ans=0;
    for(int i=2; i<=m; i++)
    {
        ans+=m-sa[i-1]-height[i];
    }
    ans+=m-sa[m];
    LL last=-1;
    for(int i=0; i<m; i++)
    {
        if(p[i]>=39)
        {
            if(last==-1)
                last=i;
            else
            {
                ans-=(i-last)*(last+1);
                last=i;
            }
        }
    }
    if(last!=-1)
        ans-=(m-last)*(last+1);
    return ans;
}
int main()
{
    int t;
    cin>>t;
    int kk=1;
    while(t--)
    {

        scanf("%d",&n);
        scanf("%s",s);
        int len=strlen(s);
        int sum=0;
        for(int i=0; i<len; i++)
            num[sum++]=s[i]-'a'+1;
        int hh=39;
        num[sum++]=hh++;
        for(int i=0; i<n; i++)
        {
            scanf("%s",s);
            int le=strlen(s);
            for(int j=0; j<le; j++)
            {
                num[sum++]=s[j]-'a'+1;
            }
            if(i!=n-1)
                num[sum++]=hh++;
        }
        num[sum]=0;
        LL ans1=get(num,sum,hh);
        LL ans2=get(num+len+1,sum-len-1,hh);
        printf("Case %d: ",kk++);
        cout<<ans1-ans2<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zsychanpin/p/6711016.html