您的位置:首页 > 其它

BZOJ3676: [Apio2014]回文串

2016-02-11 22:05 615 查看
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我不想打= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: