zoukankan      html  css  js  c++  java
  • 聊聊回文自动机

    http://www.360doc.com/content/19/0120/11/5315_810146088.shtml

    (这是一篇很好的入门博客)

    (1)

    HDU 3948

    给出一个字符串,求其不相同回文子串的个数

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 100005 ;
    const int N = 26 ;
    typedef long long LL;
    struct Palindromic_Tree {
        int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
        int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
        LL cnt[MAXN] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
        int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
        int len[MAXN] ;//len[i]表示节点i表示的回文串的长度(一个节点表示一个回文串)
        int S[MAXN] ;//存放添加的字符
        int last ;//指向新添加一个字母后所形成的最长回文串表示的节点。
        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 ) {
            c -= 'a' ;
            S[++ n] = c ;
            int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
            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 ;
            }
            last = next[cur][c] ;
            cnt[last] ++ ;
        }
    
        void count () {
            for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
            //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        }
    }T;
    
    int main()
    {
        std::ios::sync_with_stdio(false);
        string a;
        int t;
        int ct=1;
        cin>>t;
        while(t--)
        {
            cin>>a;
            T.init();
            int len=a.size();
            for(int i=0;i<len;i++)
                T.add(a[i]);
            cout<<"Case #"<<ct++<<": ";
            cout<<T.p-2<<endl;     //输出新增结点个数即可
        }
    }
    View Code

    (2)

    BZOJ3676: [Apio2014]回文串

    考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出 
    现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最 
    大出现值。

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 300000+10 ;
    const int N = 26 ;
    typedef long long LL;
    struct Palindromic_Tree {
        int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
        int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
        LL cnt[MAXN] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
        int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
        int len[MAXN] ;//len[i]表示节点i表示的回文串的长度(一个节点表示一个回文串)
        int S[MAXN] ;//存放添加的字符
        int last ;//指向新添加一个字母后所形成的最长回文串表示的节点。
        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 ) {
            c -= 'a' ;
            S[++ n] = c ;
            int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
            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 ;
            }
            last = next[cur][c] ;
            cnt[last] ++ ;
        }
    
        void count () {
            for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
            //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        }
    }T;
    char a[MAXN];
    int main()
    {
    
    
            scanf("%s",a);
            T.init();
            int len=strlen(a);
            for(int i=0;i<len;i++)
                T.add(a[i]);
                T.count();
            long long ans=0;
            for(int i=2 ; i<=T.p-1 ; i++)
            ans=max(ans,(long long)1*T.cnt[i]*T.len[i]);
            printf("%lld
    ",ans);
    }
    View Code

     (3)

    https://hihocoder.com/problemset/problem/1589

    给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不同就算作不同的子串),有多少个是回文字符串?

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 800000+10 ;
    const int N = 26 ;
    typedef long long LL;
    struct Palindromic_Tree {
        int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
        int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
        LL cnt[MAXN] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
        int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
        int len[MAXN] ;//len[i]表示节点i表示的回文串的长度(一个节点表示一个回文串)
        int S[MAXN] ;//存放添加的字符
        int last ;//指向新添加一个字母后所形成的最长回文串表示的节点。
        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 ) {
            c -= 'a' ;
            S[++ n] = c ;
            int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
            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 ;
            }
            last = next[cur][c] ;
            cnt[last] ++ ;
        }
    
        void count () {
            for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
            //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        }
    }T;
    char a[MAXN];
    int main()
    {
    
    
            scanf("%s",a);
            T.init();
            int len=strlen(a);
            for(int i=0;i<len;i++)
                T.add(a[i]);
                T.count();
            long long ans=0;
            for(int i=2 ; i<=T.p-1 ; i++)
            ans+=T.cnt[i];
            printf("%lld
    ",ans);
    }
    View Code

    (4)

    给你一个字符串 s,求本质不同的回文字符串的加和 mod 1e9+7。 
    例:s = “1111”, ans = 1111 + 111 + 11 + 1. 

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <set>
    #include <map>
    #include <bitset>
    #include <algorithm>
    #define pr pair<int,int>
    #define fi first
    #define se second
    #define mp make_pair
    #define ll long long
    using namespace std;
    const int MAXN = 2000009 ;
    const int N = 11 ;
    const ll mod = 1e9 + 7;
    ll P[MAXN];
    ll num[MAXN];
    struct Palindromic_Tree {
        int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
        int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
        int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
        int S[MAXN] ;//存放添加的字符
        int last ;//指向上一个字符所在的节点,方便下一次add
        int n ;//字符数组指针
        int p ;//节点指针
        ll ans = 0;
    
        int newnode ( int l ) {//新建节点
            for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
            len[p] = l ;
            return p ++ ;
        }
    
        void init () {//初始化
            p = 0 ;
            ans = 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) {
            //c -= 'a' ;
            S[++n] = c ;
            int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
            if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
                int now = newnode ( len[cur] + 2 ) ;//新建节点
                ans = (ans + num[n] - num[n-len[now]] * P[len[now]] % mod  + mod ) % mod;
                fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
                next[cur][c] = now ;
                //cout<< num[i] <<' '<< num[i-len[now]] * P[len[now]]<<endl;
    
            }
            last = next[cur][c] ;
        }
    
    }pam;
    
    char buf[MAXN];
    int main()
    {
        scanf("%s",buf + 1);
        P[0] = 1;
        num[0] = 0;
        int n = strlen(buf+1);
        for(int i = 1;i<=n;i++)
        {
            num[i] = (num[i-1] * 10 + 1LL*(buf[i] - '0'))% mod;
            P[i] = P[i-1]*10% mod;
        }
    //    for(int i = 1;i<=n;i++)
    //    {
    //        printf("%d
    ",P[i]);
    //    }
        pam.init();
        for(int i = 1;i<=n;i++)
        {
            pam.add(buf[i] - '0');
        }
        printf("%lld
    ",pam.ans);
        return 0;
    
    }
    View Code
  • 相关阅读:
    关系数据理论
    JavaScript语言——对象
    网络编程基础入门级
    数据库加快查询速度索引
    C/C++随机函数的生成(转载)
    sql连接查询
    深入浅出HTTP请求
    17搜索如何抓全网页
    搜索引擎之百度一下
    搜索引擎之中搜
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/11272459.html
Copyright © 2011-2022 走看看