SPOJ 题目 8222 NSUBSTR - Substrings(后缀自动机+DP求子串出现最大次数)
2015-09-03 02:22
489 查看
NSUBSTR - Substrings
no tagsYou are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will
be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Example
Input: ababa Output: 3 2 2 1 1 题目大意:问这个字符串长度为1~len的子串出现的最大次数分别是多少 ac代码#include<stdio.h> #include<stdlib.h> #include<string.h> #define max(a,b) (a>b?a:b) #define N 510005 struct sam { sam *pre,*son[26]; int len,g; }que ,*root,*tail,*b ; int tot; void add(int c,int l) { sam *p=tail,*np=&que[tot++]; np->len=l; tail=np; while(p&&p->son[c]==NULL) { p->son[c]=np; p=p->pre; } if(p==NULL) np->pre=root; else { sam *q=p->son[c]; if(p->len+1==q->len) np->pre=q; else { sam *nq=&que[tot++]; *nq=*q; nq->len=p->len+1; np->pre=q->pre=nq; while(p&&p->son[c]==q) { p->son[c]=nq; p=p->pre; } } } } char str[N>>1]; int dp[N>>1]; int main() { while(scanf("%s",str)!=EOF) { int len=strlen(str); tot=0; root=tail=&que[tot++]; int i,j; for(i=0;i<len;i++) add(str[i]-'a',i+1); int cnt[N>>1]; memset(cnt,0,sizeof(cnt)); for(i=0;i<tot;i++) cnt[que[i].len]++; for(i=1;i<=len;i++) cnt[i]+=cnt[i-1]; for(i=0;i<tot;i++) b[--cnt[que[i].len]]=&que[i]; for(i=0;i<len;i++) { (root=root->son[str[i]-'a'])->g++; //root->g++; } memset(dp,0,sizeof(dp)); for(i=tot-1;i>=0;i--) { dp[b[i]->len]=max(dp[b[i]->len],b[i]->g); if(b[i]->pre) b[i]->pre->g+=b[i]->g; } for(i=len-1;i>0;i--) dp[i]=max(dp[i],dp[i+1]); for(i=1;i<=len;i++) printf("%d\n",dp[i]); } }
相关文章推荐
- Java____堆内存和栈内存
- 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
- 堆
- php大力力 [043节] 现在要做个删除前的提示功能
- 对于可重入、线程安全、异步信号安全几个概念的理解
- Linux学习之grep篇
- Hadoop学习计划
- 算法----归并排序
- 犀牛——第8章函数 8.6闭包
- 给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数
- Clustered Shading架构实现步骤
- J2SE——JDK安装和环境变量配置
- hdu4462 Scaring the Birds || 2012 Asia Hangzhou Regional Contest || 简单暴力
- 重写、覆盖、重载、多态几个概念的区别
- Leetcode #232 Implement Queue using Stacks
- wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流
- 通过box盒子模型给元素内容设置居中
- 开发日记:接口开发设计
- PHP date_default_timezone_set()函数之中国时区设定
- Longest Substrings Without Repeating Characters