BZOJ3676: [Apio2014]回文串

PAM裸题
关于PAM怎么构造我之前有个链接
然后就是每次加num就好了
最后统计一下
我打的代码常数打的吓人
PAM:

#include<cstdio>
#include<iostream>
#include<cstring>
using  namespace std;

struct Node
{
  int len;
  Node *ch[31];
  Node *last;
  int num;
  Node(){for(len=0;len<=30;len++)ch[len]=NULL;last=NULL;len=num=0;}
};
Node*Max_Node,*Node_2,*Node_1;
int num;
Node *Stack[400001];
int top;
inline Node *New_Node()
{
    Node *res=new Node;
    Stack[top++]=res;
    return res;
}
char s[400001];
inline void begin(){Node_2=new Node;Node_1=new Node;Max_Node=Node_2;num=2;Node_2->last=Node_1->last=Node_1;Node_1->len=-1;Node_2->len=0;}
inline bool add(int place)
{
    bool Exit=false;
    Node *cur=Max_Node;
    int curlen=0,son=s[place]-'a';
    while(true)
      {
         curlen=cur->len;
         if(place-1-curlen>=0&&s[place-1-curlen]==s[place])
             break;
          cur=cur->last;
      }
    if(cur->ch[son])
         Max_Node=cur->ch[son],cur->ch[son]->num++; 
    else
     {
         Exit=true;
         Node*tp=New_Node();
         Max_Node=tp;
         tp->len=cur->len+2;
         cur->ch[son]=tp;
         if(tp->len==1)
            tp->last=Node_2,tp->num=1;
         else
          {
             while(true)
                {
                    cur=cur->last;
                    curlen=cur->len;
                    if(place-1-curlen>=0&&s[place-1-curlen]==s[place])
                      {
                        Max_Node->last=cur->ch[son];
                        break;
                      }
                }
            Max_Node->num=1;             
          }  
    }
    return Exit;
}

long long ans=-1;
inline void Calc()
{
    Node *tp;
    for(int i=top-1;i!=-1;i--)
      {
        tp=Stack[i];tp->last->num+=tp->num;
        ans=max(ans,tp->num*1ll*tp->len);
      }
}
char c;
int main()
{
    int len=0;
    do c=getchar();while(c<'a'||c>'z');
    while(c<='z'&&c>='a')
       s[len++]=c,c=getchar();
    begin();
    for(int i=0;i<len;i++)add(i);
    Calc();
    printf("%lld\n",ans);
    return 0;
} 

还有一种方法很丑 是用manacher+SAM我不想打= =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值