3676: [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。

 

题意:给定一个字符串s,求所有回文子串中 回文字符串的长度与出现次数的最大值

 

#include<iostream>
#include<stdio.h>
#include <algorithm>
#include <string>
#include<string.h>
#include<math.h>
#define  ll long long
using namespace std;
const int MAXN = 300005 ;
const int N = 26 ;
char s[MAXN];//输入的要处理的字符串
struct Palindromic_Tree
{
     int next[MAXN][26] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
     int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点

     //回文树里面的一个节点就代表一个回文串

     int cnt[MAXN] ;//表示第i个节点代表的回文串出现的次数
     int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
     int len[MAXN] ;//表示第i个节点代表的回文串长度

     int S[MAXN] ;//存放添加的字符
     int last ;//指向上一个字符所在的节点,方便下一次add
     int n ;//字符数组指针
     int p ;//节点指针
     int newnode(int l)     //在树中新建节点
     {
           for(int i = 0 ; i < N ; ++ i) next[p][i] = 0 ;
           cnt[p] = 0 ;
           num[p] = 0 ;
           len[p] = l ;
           return p ++ ;
     }
     void init()   //初始化
     {
           p = 0 ;
           newnode(0) ;//建一棵保存长度为偶数的回文树
           newnode(-1) ;//长度为奇数的回文树
           last = 0 ;
           n = 0 ;
           S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
           fail[0] = 1 ;
     }
     int get_fail(int x)     //和KMP一样,失配后找一个尽量最长的
     {
          while(S[n - len[x] - 1] != S[n]) 
              x = fail[x] ;
          return x ;
     }
     void add(int c,int pos)
     {
           //printf("%d:",p);//输出节点编号
           c -= 'a';
           S[++ n] = c ;
           int cur = get_fail(last) ;   //通过上一个回文串找这个回文串的匹配位置
           //printf("%d ",cur);//输出节点编号p代表的回文串的字符长度
           if(!next[cur][c])     //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
           {
                 int now = newnode(len[cur] + 2) ;   //新建节点
                 fail[now] = next[get_fail(fail[cur])][c] ;   //和AC自动机一样建立fail指针,以便失配后跳转
                 next[cur][c] = now ;
                 num[now] = num[fail[now]] + 1 ;
                 /*for(int i=pos-len[now]+1; i<=pos; ++i)//输出编号为P代表的回文串 
                    printf("%c",s[i]);*/
           } 
           last = next[cur][c] ;
           cnt[last] ++ ;
           //putchar(10);//输出回车换行
     }
     void count()
     {
           for(int i = p - 1 ; i >= 0 ; -- i) 
                cnt[fail[i]] += cnt[i] ;
           //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
     }
} pat;
int main()
{
     scanf("%s",s);
     int n=strlen(s);
     pat.init();
     for(int i=0; i<n; i++) 
          pat.add(s[i],i);
     pat.count();
     ll ans=0;
     for(int i=1;i<pat.p;i++)
        ans=max(ans,(ll)pat.len[i]*pat.cnt[i]);
    printf("%lld\n",ans);
     return 0;
}

3676: [Apio2014]回文串 

posted @ 2019-09-18 09:42  知道了呀~  阅读(304)  评论(0编辑  收藏  举报