码迷,mamicode.com
首页 > 其他好文 > 详细

回文树(模板)

时间:2019-09-17 22:39:39      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:define   tree   bsp   字典树   --   www   fail   i++   size   

一、复杂度

    构造回文树需要的空间复杂度为O(N*字符集大小),时间复杂度为O(N*log(字符集大小))

 

二、应用

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

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

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

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

 

三、简单说明

int next[MAXN][26] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
     int fail[i] ;//fail指针,失配后跳转到fail指针指向的节点

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

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

     int S[i] ;//存放添加的字符
     int last ;//指向上一个字符所在的节点,方便下一次add
     int n ;//字符数组指针
     int p ;//节点指针

 

实现过程详解:

  https://www.cnblogs.com/crazyacking/p/4742400.html

 

四、代码

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;
const int MAXN = 100005 ;
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);
           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) 
                    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的子回文串!
     }
} run;
int main()
{
     scanf("%s",&s);
     int n=strlen(s);
     run.init();
     for(int i=0; i<n; i++) 
          run.add(s[i],i);
     run.count();
     return 0;
}

 

回文树(模板)

标签:define   tree   bsp   字典树   --   www   fail   i++   size   

原文地址:https://www.cnblogs.com/-citywall123/p/11537444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!