• 【BZOJ3676】[Apio2014]回文串 回文自动机


    【BZOJ3676】[Apio2014]回文串

    Description

    考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最 大出现值。 

    Input

    输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。 

    Output

    输出一个整数,为逝查回文子串的最大出现值。 

    Sample Input

    【样例输入l】
    abacaba
    【样例输入2]
    www

    Sample Output

    【样例输出l】
    7
    【样例输出2]
    4

    HINT

    一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。 
    在第一个样例中,回文子串有7个:a,b,c,aba,aca,bacab,abacaba,其中: 
    ● a出现4次,其出现值为4:1:1=4 
    ● b出现2次,其出现值为2:1:1=2 
    ● c出现1次,其出现值为l:1:l=l 
    ● aba出现2次,其出现值为2:1:3=6 
    ● aca出现1次,其出现值为1=1:3=3 
    ●bacab出现1次,其出现值为1:1:5=5 
    ● abacaba出现1次,其出现值为1:1:7=7 
    故最大回文子串出现值为7。 
    【数据规模与评分】 
    数据满足1≤字符串长度≤300000。

    题解:初学回文自动机,每个回文串出现的次数就是fail树上子树的权值和。

    注意第17行,别问我为什么~

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int maxn=600010;
    typedef long long ll;
    int n,tot,last;
    ll ans;
    char str[maxn],ss[maxn];
    int ch[maxn][27],fail[maxn],len[maxn],cnt[maxn];
    void extend(int now,int x)
    {
    	int p=last;
    	while(str[now]!=str[now-len[p]-1])	p=fail[p];
    	if(ch[p][x])	{	last=ch[p][x];	return ;	}
    	int np=++tot,tmp=p;	len[np]=len[p]+2,p=fail[p];
    	while(p&&str[now]!=str[now-len[p]-1])	p=fail[p];
    	if(!p)	fail[np]=1;
    	else	fail[np]=ch[p][x];
    	ch[tmp][x]=last=np;
    }
    int main()
    {
    	scanf("%s",ss);
    	int i,l=strlen(ss);
    	for(i=0;i<l;i++)	str[n++]='{',str[n++]=ss[i];
    	str[n++]='{';
    	last=tot=1,len[1]=-1;
    	for(i=0;i<n;i++)	extend(i,str[i]-'a'),cnt[last]++;
    	for(i=tot;i>1;i--)	cnt[fail[i]]+=cnt[i],ans=max(ans,(ll)cnt[i]*(len[i]>>1));
    	printf("%lld",ans);
    	return 0;
    }//ababcbc
  • 相关阅读:
    shell截取字符串的方法
    安装sql server managerment studio报错"The instance id is required but it is missing"
    windows 80端口被占用的解决方法
    centos如何安装软件
    ORA-00257归档日志写满的解决方法
    VCenter克隆虚拟机报错msg.snapshot.error-QUIESCINGERROR
    如何启动或关闭oracle的归档(ARCHIVELOG)模式
    ubuntu下nagios配置
    vsphere vcenter server下安装ubuntu的vmwaretools
    virtualbox迁移至vcenter/vmware workstation
  • 原文地址:https://www.cnblogs.com/CQzhangyu/p/7366952.html
Copyright © 2020-2023  润新知