zoukankan      html  css  js  c++  java
  • 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树

    The Problem to Slow Down You

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93645#problem/G

    Description

    http://7xjob4.com1.z0.glb.clouddn.com/76e435d3c21fa3cbeef1e76780086dc4

    Input

     

    Output

    
    

    Sample Input

     

    Sample Output

     

    HINT

    题意

    给你两个字符串,然后问你这两字符串中 有多少对本质不同的字符串子序列

    题解:

    回文树,参考 http://blog.csdn.net/u013368721/article/details/42100363

    建完树之后,DFS一波就行,模板题

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<map>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    #define maxn 200005
    
    const int MAXN = 200005 ;
    const int N = 36 ;
    /*
    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.p表示添加的节点个数。
    9.n表示添加的字符个数。
    */
    struct Palindromic_Tree
    {
        int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
        int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
        int cnt[MAXN] ;
        int num[MAXN] ;
        int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
        int S[MAXN] ;//存放添加的字符
        int last ;//指向上一个字符所在的节点,方便下一次add
        int n ;//字符数组指针
        int p ;//节点指针
        int val[MAXN] ;
    
        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+=1;
            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] ++ ;
        }
    
        int query( int c )
        {
            c+=1;
            c-='a';
            int cur = get_fail(last);
            if(!next[cur][c])
                return 0;
            last = next[cur][c];
            return cnt[last];
        }
    
        void count () {
            for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
            //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        }
    } L,R;
    
    
    char s1[maxn];
    char s2[maxn];
    long long ans=0;
    void dfs(int x,int y)
    {
        for(int j=0;j<35;j++)
        {
            int xx = L.next[x][j];
            int yy = R.next[y][j];
            if(xx&&yy)
            {
                ans += (long long) ( L.cnt[xx] * 1LL * R.cnt[yy] * 1LL ) ;
                dfs(xx,yy);
            }
        }
    }
    int main()
    {
        int t;scanf("%d",&t);
        for(int cas=1;cas<=t;cas++)
        {
            L.init();
            R.init();
            scanf("%s",s1);
            int len = strlen(s1);
            for(int i=0;i<len;i++)
                L.add(s1[i]);
            L.count();
            scanf("%s",s2);
            len = strlen(s2);
                ans = 0;
            for(int i=0;i<len;i++)
                R.add(s2[i]);
            R.count();
            dfs(0,0);
            dfs(1,1);
            printf("Case #%d: %lld
    ",cas,ans);
        }
    }
  • 相关阅读:
    事务的隔离级别
    事务的隔离
    事务简介
    leetcode647
    leetcode394
    leetcode96
    leetcode814
    leetcode738
    leetcode621
    leetcode763
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4854662.html
Copyright © 2011-2022 走看看