zoukankan      html  css  js  c++  java
  • 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习

    写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher。然后就码码码。。。过是过了,然后看一下[Status],怎么慢这么多,不服。。然后就搜了一下,发现一种新东西——回文树。

     

    回文树的功能:

    假设我们有一个串S,S下标从0开始,则回文树能做到如下几点:

    1.求串S前缀0~i内本质不同回文串的个数(两个串长度不同或者长度相同且至少有一个字符不同便是本质不同)

    2.求串S内每一个本质不同回文串出现的次数

    3.求串S内回文串的个数(其实就是1和2结合起来)

    4.求以下标i结尾的回文串的个数

     

    构造回文树

    首先定义变量

    1.len[i] 表示编号为i的节点表示的回文串的长度(一个节点表示一个回文串)

    2.next[i][c] 表示编号为i的节点表示的回文串在两边添加字符c以后变成的回文串的编号(和字典树类似)

    3.fail[i] 表示节点i失配以后跳转不等于自身的节点i表示的回文串的最长后缀回文串(和AC自动机类似)

    4.cnt[i] 表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)

    5.num[i] 表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数

    6.last 指向新添加一个字母后所形成的最长回文串表示的节点。

    7.S[i] 表示第i次添加的字符(一开始设S[0] = -1(可以是任意一个在串S中不会出现的字符))

    8.表示添加的节点个数

    9.n表示添加的字符个数

     

    一开始回文树有两个节点,0表示偶数长度串的根和1表示奇数长度串的根,且len[0]=0,len[1]=-1,last=0,S[0]=-1,n=0,p=2(添加了节点0、1)。

    假设现在我们有串S = abbaabba。

    首先我们添加第一个字符'a',S[++n]='a',然后判断此时S[n-len[last]-1]是否等于S[n],即上一个串-1的位置和新添加的位置是否相同,相同则说明构成回文。

    否则,last = fail[last]。

    此时last=0,我们发现S[1-0-1]!=S[1],所以last=fail[last]=1,然后我们发现S[1-(-1)-1] == S[1](即自己等于自己,所以我们让len[1]等于-1可以让这一步更加方便)。

     

    令cur等于此时的last(即 cur=last=1),判断此时next[cur]['a']是否已经有后继。

    如果next[cur]['a']没有后继,我们就进行如下的步骤

    新建节点(节点数p++,且之后p=3),并让now等于新节点的编号(now=2),则len[now]=len[cur]+2(每一个回文串的长度总是在其最长子回文串的基础上在两边加上两个相同的字符构成的,所以是+2,同时体现出我们让len[1]=-1的优势,一个字符自成一个奇回文串时回文串的长度为(-1)+2=1)。

    然后我们让fail[now]=next[get_fail(fail[cur] )]['a'],即得到fail[now](此时为fail[2]=0),其中的get_fail函数就是让找到第一个使得S[n-len[last]-1]==S[n]的last。

    然后next[cur]['a']=now。

    当上面步骤完成后我们让last=next[cur][c](不管next[cur]['a']是否有后继),然后cnt[last] ++。

     

    此时回文树为下图状态:

    现在我们添加第二个字符字符'b'到回文树中:

    继续添加第三个字符'b'到回文树中:

    继续添加第四个字符'a'到回文树中:

    继续添加第五个字符'a'到回文树中:

    继续添加第六个字符'b'到回文树中:

    继续添加第七个字符'b'到回文树中:

    继续添加第八个字符'a'到回文树中:

     

     

    到此,串S已经完全插入到回文树中了,现在所有的数据如下:

    然后我们将节点x在fail指针树中将自己的cnt累加给父亲,从叶子开始倒着加,最后就能得到串S中出现的每一个本质不同回文串的个数。

     

    构造回文树需要的空间复杂度为O(N*字符集大小),时间复杂度为O(N*log(字符集大小)),这个时间复杂度比较神奇。如果空间需求太大,可以改成邻接表的形式存储,不过相应的要牺牲一些时间。

    总的来说,这是一个很好的算法

     

    模板:

     1 const int MAXN = 100005 ;  
     2 const int N = 26 ;  
     3   
     4 struct Palindromic_Tree {  
     5     int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成  
     6     int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点  
     7     int cnt[MAXN] ;  
     8     int num[MAXN] ;  
     9     int len[MAXN] ;//len[i]表示节点i表示的回文串的长度  
    10     int S[MAXN] ;//存放添加的字符  
    11     int last ;//指向上一个字符所在的节点,方便下一次add  
    12     int n ;//字符数组指针  
    13     int p ;//节点指针  
    14   
    15     int newnode ( int l ) {//新建节点  
    16         for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;  
    17         cnt[p] = 0 ;  
    18         num[p] = 0 ;  
    19         len[p] = l ;  
    20         return p ++ ;  
    21     }  
    22   
    23     void init () {//初始化  
    24         p = 0 ;  
    25         newnode (  0 ) ;  
    26         newnode ( -1 ) ;  
    27         last = 0 ;  
    28         n = 0 ;  
    29         S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判  
    30         fail[0] = 1 ;  
    31     }  
    32   
    33     int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的  
    34         while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;  
    35         return x ;  
    36     }  
    37   
    38     void add ( int c ) {  
    39         c -= 'a' ;  
    40         S[++ n] = c ;  
    41         int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置  
    42         if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串  
    43             int now = newnode ( len[cur] + 2 ) ;//新建节点  
    44             fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转  
    45             next[cur][c] = now ;  
    46             num[now] = num[fail[now]] + 1 ;  
    47         }  
    48         last = next[cur][c] ;  
    49         cnt[last] ++ ;  
    50     }  
    51   
    52     void count () {  
    53         for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;  
    54         //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!  
    55     }  
    56 } ;
    Palindromic Tree

     

    下面是一些题目,感兴趣的话可以做一下~

    1.ural1960. Palindromes and Super Abilities

    2.TsinsenA1280. 最长双回文串

    3.TsinsenA1255. 拉拉队排练

    4.TsinsenA1393. Palisection

    5.2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You

     

    转载 from:http://blog.csdn.net/u013368721/article/details/42100363

     

     接下来是【Apio2014】回文串 

     1 /**************************************************************
     2     Problem: 3676
     3     User: YJY
     4     Language: C++
     5     Result: Accepted
     6     Time:756 ms
     7     Memory:46712 kb
     8 ****************************************************************/
     9  
    10 #include<algorithm>
    11 #include<iostream>
    12 #include<cstdlib>
    13 #include<cstring>
    14 #include<cstdio>
    15 #include<cmath>
    16 using namespace std;
    17  
    18 typedef long long LL;
    19  
    20 #define N 330010
    21  
    22 int next[N][30];
    23 int fail[N];
    24 int cnt[N];
    25 int num[N];
    26 int len[N];
    27 int s[N];
    28 int last;
    29 int p;
    30 int n;
    31  
    32 char str[N];
    33  
    34 LL ans=1;
    35  
    36 int newnode(int x)
    37 {
    38     for (int i=0;i<30;i++)
    39         next[p][i]=0;
    40     cnt[p]=0;
    41     num[p]=0;
    42     len[p]=x;
    43     return p++;
    44 }
    45  
    46 void init()
    47 {
    48     p=0;
    49     newnode(0);
    50     newnode(-1);
    51     last=0;
    52     n=0;
    53     s[0]=-1;
    54     fail[0]=1;
    55 }
    56  
    57 int get_fail(int x)
    58 {
    59     while (s[n-len[x]-1]!=s[n])
    60         x=fail[x];
    61     return x;
    62 }
    63  
    64 void add(int c)
    65 {
    66     c-='a';
    67     s[++n]=c;
    68     int cur=get_fail(last);
    69     if (!next[cur][c])
    70     {
    71         int now=newnode(len[cur]+2);
    72         fail[now]=next[get_fail(fail[cur])][c];
    73         next[cur][c]=now;
    74         num[now]=num[fail[now]]+1;
    75     }
    76     last=next[cur][c];
    77     cnt[last]++;
    78 }
    79  
    80 void count()
    81 {
    82     for (int i=p-1;i>=0;i--)
    83         cnt[fail[i]]+=cnt[i];
    84 }
    85  
    86 int main()
    87 {
    88     scanf("%s",str);
    89     init();
    90     int sz=strlen(str);
    91     for (int i=0;i<sz;i++)
    92         add(str[i]);
    93     count();
    94     for (int i=2;i<p;i++)
    95         ans=max(ans,1LL*len[i]*cnt[i]);
    96     printf("%lld
    ",ans);
    97     return 0;
    98 }
    View Code

     

  • 相关阅读:
    数据库笔记(mysql)(1)
    微擎框架商业版 V2.1.2 去后门一键安装版+去除云平台+无附带模块
    资深架构师Sum的故事:正则!入门就是这样简单
    人脸识别+大数据再结合面相学,看相一看一个准!
    layui里面的layer模块弹窗,强制居中的方法!!!
    抖音获取视频点赞数、播放数、获取用户粉丝列表
    破解微擎安装,免费搭建微擎,免费破解微擎,微擎破解版本,最新版本V2.1.2,一键安装!!
    资深架构师Sum的故事:(Mysql)InnoDB下,存储过程中事务的处理
    小程序开发教程:wx.setTopBarText(OBJECT)
    微信web协议,群成员唯一uin,获取群成员唯一标识
  • 原文地址:https://www.cnblogs.com/yangjiyuan/p/5470127.html
Copyright © 2011-2022 走看看