Boring counting

40 篇文章 0 订阅
28 篇文章 9 订阅

题目描述

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”). 

输入描述:

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).

输出描述:

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.
示例1

输入

aaaa
ababcabb
aaaaaa
#
       

输出

2
3
3
#include<stdio.h>
#include<string.h>
#define max 11000
int wa[max],wb[max],wv[max],ws[max];
int rank[max],height[max];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;
    for(p=1,j=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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
    return ;
}
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++)rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
    return;
}
int main()
{
    char str1[max];
    int str[max];
    int sa[max];
    int mint;
    int maxt;
    while(scanf("%s",str1)!=EOF)
    {
        int len=strlen(str1);
        if(str1[0]=='#')
            break;
        for(int i=0;i<len;i++)
            str[i]=str1[i]-'a'+1;
        str[len]=0;
        da(str,sa,len+1,27);
        calheight(str,sa,len);
        int ans=0;
        for(int i=1; i<=(len+1)/2; i++)
        {
            mint=1001;
            maxt=-1;
            for(int j=1; j<=len; j++)
            {
                if(height[j]>=i)
                {
                    int tem=sa[j-1]>sa[j]?sa[j]:sa[j-1];
                    mint=mint>tem?tem:mint;
                        tem=sa[j-1]>sa[j]?sa[j-1]:sa[j];
                    maxt=maxt>tem?maxt:tem;                }
                else
                {
                    if(mint+i<=maxt)ans++;
                    mint=1001,maxt=-1;
                }
            }
            if(mint+i<=maxt)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值