HDOJ 3518 Boring counting

本文介绍了一种基于字符串匹配自动机(SAM)的算法实现,通过构建SAM来高效地寻找字符串中出现至少两次且不相互重叠的子串。文章详细解释了SAM的基本操作,包括节点的最左出现位置、最右出现位置及出现次数等关键概念,并提供了一个完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


SAM基本操作 拓扑求每个节点的  最左出现left,最右出现right,出现了几次num ......

对于每一个出现两次以上的节点,对其所对应的一串子串的长度范围 [fa->len+1,len] 和其最大间距 right-left比较

即可......

Boring counting

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


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
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int CHAR=26,maxn=1100;

struct SAM_Node
{
    SAM_Node *fa,*next[CHAR];
    int len,id,pos;
    SAM_Node(){}
    SAM_Node(int _len)
    {
        len=_len;
        fa=0; memset(next,0,sizeof(next));
    }
};

SAM_Node SAM_node[maxn*2],*SAM_root,*SAM_last;
int SAM_size;

SAM_Node *newSAM_Node(int len)
{
    SAM_node[SAM_size]=SAM_Node(len);
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
}

SAM_Node *newSAM_Node(SAM_Node *p)
{
    SAM_node[SAM_size]=*p;
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
}

void SAM_init()
{
    SAM_size=0;
    SAM_root=SAM_last=newSAM_Node(0);
    SAM_node[0].pos=0;
}

void SAM_add(int x,int len)
{
    SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
    np->pos=len; SAM_last=np;
    for(;p&&!p->next[x];p=p->fa)
        p->next[x]=np;
    if(!p)
    {
        np->fa=SAM_root;
        return ;
    }
    SAM_Node *q=p->next[x];
    if(q->len==p->len+1)
    {
        np->fa=q;
        return ;
    }
    SAM_Node *nq=newSAM_Node(q);
    nq->len=p->len+1;
    q->fa=nq; np->fa=nq;
    for(;p&&p->next[x]==q;p=p->fa)
        p->next[x]=nq;
}

char str[maxn];
int len,c[maxn],L[maxn*2],R[maxn*2],num[maxn*2];
SAM_Node *top[maxn*2];

int main()
{
while(scanf("%s",str)!=EOF)
{
    if(str[0]=='#') break;
    SAM_init();
    len=strlen(str);
    for(int i=0;i<len;i++)
        SAM_add(str[i]-'a',i+1);

    memset(c,0,sizeof(c)); memset(top,0,sizeof(top));
    memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(num,0,sizeof(num));

    ///get tupo sort
    for(int i=0;i<SAM_size;i++)
        c[SAM_node[i].len]++;
    for(int i=1;i<=len;i++)
        c[i]+=c[i-1];
    for(int i=0;i<SAM_size;i++)
        top[--c[SAM_node[i].len]]=&SAM_node[i];

    ///get L,R,num
    SAM_Node *p=SAM_root;
    for(;p->len!=len;p=p->next[str[p->len]-'a'])
    {
        num[p->id]=1;
        L[p->id]=R[p->id]=p->len;
    }
    for(int i=SAM_size-1;i>=0;i--)
    {
        p=top[i];
        if(L[p->id]==0&&R[p->id]==0)
        {
            L[p->id]=R[p->id]=p->pos;
        }
        if(p->fa)
        {
            SAM_Node *q=p->fa;
            num[q->id]+=num[p->id];
            if(L[q->id]==0||L[q->id]>L[p->id])
                L[q->id]=L[p->id];
            if(R[q->id]==0||R[q->id]<R[p->id])
                R[q->id]=R[p->id];
        }
    }
    int ans=0;
    for(int i=1;i<SAM_size;i++)
    {
        int ma=SAM_node[i].len;
        int mi=SAM_node[i].fa->len+1;
        int le=R[SAM_node[i].id]-L[SAM_node[i].id];
        if(le>=ma)
            ans+=ma-mi+1;
        else if(le>mi)
            ans+=le-mi+1;
    }
    printf("%d\n",ans);
}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值