【51Nod1292】字符串中的最大值
来源:互联网 发布:编程书 编辑:程序博客网 时间:2024/06/05 02:02
Description
有一个字符串T。字符串S的F函数值可以如下计算:F(S) = L * S在T中出现的次数(L为字符串S的长度)。求所有T的子串S中,函数F(S)的最大值。
Solution
也是后缀自动机的经典操作。
我打的这题才发现,我打字符串中的最大值是有一个bug,不过有这个bug也可以拿好多分。
在每个串出现的次数的初始化时,只有每个1~n对应的点才有值,很显然(即cc[np]=1)。
还有一个很坑的就是在c++的函数里面用强制转换long long的操作会有问题,比如说max(ll(t[i].len*cc[i]),ans)会有问题,要把左边的打出来。
Code
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define fo(i,a,b) for(i=a;i<=b;i++)#define fod(i,a,b) for(i=a;i>=b;i--)using namespace std;typedef long long ll;const int maxn=2000007;int i,j,k,l,n,m;int np,nq,p,q,last,x,num;int a[maxn],b[maxn],cc[maxn];ll ans1,ans;struct node{ int son[26],len,fa;}t[maxn];char s[maxn];void extend(int c){ p=last,np=++num; t[np].len=t[p].len+1;cc[np]=1; while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].fa; if(!p)t[np].fa=1; else{ q=t[p].son[c]; if(t[p].len+1==t[q].len)t[np].fa=q; else{ nq=++num; t[nq]=t[q]; t[nq].len=t[p].len+1; t[q].fa=t[np].fa=nq; while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].fa; } } last=np;}int main(){// freopen("fan.in","r",stdin); scanf("%s",s+1);n=strlen(s+1); last=num=1; fo(i,1,n)extend(s[i]-'a'); fo(i,1,num)a[t[i].len]++; fo(i,1,n)a[i]+=a[i-1]; fod(i,num,1)b[a[t[i].len]--]=i; fod(i,num,1){ x=b[i]; cc[t[x].fa]+=cc[x]; } fo(i,2,num)ans1=(ll)t[i].len*cc[i],ans=max(ans1,ans); printf("%lld\n",ans);}
2 0
- 【51Nod1292】字符串中的最大值
- 【51Nod1277】字符串中的最大值
- 51NOD1277 字符串中的最大值
- [51nod 1292]字符串中的最大值V2
- 51nod-1277 字符串中的最大值(KMP)
- 51nod 1277 字符串中的最大值
- 51nod 1277 字符串中的最大值
- kmp-51nod 1277 字符串中的最大值
- 51nod 1277 字符串中的最大值
- 51Nod-1277-字符串中的最大值
- 51nod1277 字符串中的最大值(KMP)
- 51 nod 1277 字符串中的最大值(KMP)
- 51Nod-1277-字符串中的最大值
- 51nod 1277 字符串中的最大值(KMP)
- [KMP next树] 51Nod 1277 字符串中的最大值
- KMP——51nod1277 字符串中的最大值
- 51NOD 1277 字符串中的最大值 【拓展KMP】
- BZOJ 3670: [Noi2014]动物园&&51nod 1277 字符串中的最大值
- 【jzoj4777】【灌水】【搜索】
- 工厂方法模式
- IO流<笔记>
- bootstrap-typeahead的ajax调用
- HDU 5630 Rikka with Chess(构造)
- 【51Nod1292】字符串中的最大值
- wmic的官方链接
- 下一个学习计划
- Codeforces-160B-Unlucky Ticket
- java中的浅复制与深复制
- 格雷码(分治)
- 51nod 1090 3个数和为0(二分)
- 计算空间点到直线的距离
- 国内较快的maven镜像