HDU - 4416 Good Article Good sentence

题目:给一个字符串S和一系列字符串T1~Tn,问在S中有多少个不同子串满足它不是T1~Tn中任意一个字符串的子串。

思路:match[i]表示当前节点能匹配的最长长度

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
//0x3f3f3f3f

const int MAXN = 200050, SIZE = 26;
struct SAM {
    int len[MAXN], link[MAXN], next[MAXN][SIZE];
    int total, last;
    inline int newNode(int L) {
        len[++total] = L; link[total] = 0;
        for(int i = 0; i < SIZE; ++i) next[total][i] = 0;
        return total;
    }
    inline void Add(int c) {
        int i, p = last, cur = newNode(len[last] + 1);
        for(; p && !next[p][c]; p = link[p]) next[p][c] = cur;
        if(!p) link[cur] = 1;//令其指向初始状态
        else {
            int q = next[p][c];
            if(len[q] == len[p] + 1) link[cur] = q;
            else {//>
                int clone = newNode(len[p] + 1);
                for(i = 0; i < SIZE; ++i) next[clone][i] = next[q][i];
                link[clone] = link[q];
                link[q] = link[cur] = clone;
				for(; p && next[p][c] == q; p = link[p]) next[p][c] = clone;
            }
        }
        last = cur;
    }
    void Init () {//根节点是1
        total = 0;
        last = newNode(0);
    }
}sam;
char str[MAXN];
int num[MAXN],idx[MAXN],match[MAXN];
int main(){

    int T,cas=0,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        scanf("%s",str);
        sam.Init();
        for(int i=0;str[i]!='\0';i++)
            sam.Add(str[i]-'a');
        mm(num,0);
        for(int i=1;i<=sam.total;i++) num[sam.len[i]]++;
        for(int i=1;i<=sam.total;i++) num[i]+=num[i-1];
        for(int i=1;i<=sam.total;i++) idx[num[sam.len[i]]--]=i;
        mm(match,0);
        while(n--){
            scanf("%s",str);
            int now=1,len=0;
            for(int i=0;str[i]!='\0';i++){
                int c=str[i]-'a';
                if(sam.next[now][c]!=0){
                    len++;
                    now=sam.next[now][c];
                }
                else{
                    while(sam.next[now][c]==0&&now!=0) now=sam.link[now];
                    if(now==0){
                        now=1;
                        len=0;
                    }
                    else{
                        len=sam.len[now]+1;
                        now=sam.next[now][c];
                    }
                }
                match[now]=max(match[now],len);
            }
        }
        for(int i=sam.total;i>=1;i--){
            int p=idx[i],np=sam.link[p];
            if(match[p])
                match[np]=sam.len[np];
            else
                match[p]=sam.len[np];
        }
        LL sum=0;
        for(int i=1;i<=sam.total;i++)
            sum+=sam.len[i]-match[i];
        printf("Case %d: %I64d\n",++cas,sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值