zoukankan      html  css  js  c++  java
  • BZOJ3676 [Apio2014]回文串

    传送门

    回文自动机板子题~

    回文自动机和ACA以及SAM都是很类似的[毕竟都是自动机吗233]

    回文自动机的树形结构是 fail指针构成的

    用增量法 构造即可

    (其实我也没完全学懂呢T^T)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define inf 20021225
    #define ll long long
    #define mxn 310000
    using namespace std;
    
    struct node{int ch[26],len,fa,s;}t[mxn];
    // ch add character fa fail s times
    char ch[mxn]; int lt,poi,s[mxn],n; ll ans;
    void init()
    {
    	s[0]=-1;
    	t[0].len=0; t[1].len=-1;
    	t[0].fa=1; t[1].fa=1;
    	poi=lt=1;
    }
    int id(char c){return c-'a';}
    void extend(int c)
    {
    	int p=lt; s[++n]=c;
    	while(s[n-1-t[p].len] != s[n]) p=t[p].fa;
    	if(!t[p].ch[c])
    	{
    		int q=++poi; t[q].len=t[p].len+2;int np = t[p].fa;
    		while(s[n-1-t[np].len] != s[n]) np=t[np].fa;
    		t[q].fa=t[np].ch[c]; t[p].ch[c]=q; 
    	}
    	lt=t[p].ch[c]; t[lt].s++;
    }
    void calc()
    {
    	for(int i=poi;~i;i--)	t[t[i].fa].s += t[i].s;
    	for(int i=0;i<=poi;i++)	ans=max(ans,1ll*t[i].len*t[i].s);
    }
    int main()
    {
    	scanf("%s",ch); init(); int n = strlen(ch);
    	for(int i=0;i<n;i++)	extend(id(ch[i]));
    	calc(); printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    查找第K小数
    比较奇偶数个数
    哈夫曼树练习
    数字转二进制数练习
    随笔
    字符串反码(练习)
    eclipse构建maven的web项目
    mysql中的一些操作语句,留存
    urllib2功能说明
    Python-第三方库requests详解
  • 原文地址:https://www.cnblogs.com/hanyuweining/p/10321912.html
Copyright © 2011-2022 走看看