zoukankan      html  css  js  c++  java
  • HDU 3948

    #include<bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 100005 ;
    const int N = 26 ;
    
    struct Palindromic_Tree {
        int next[MAXN][N] ;
        int fail[MAXN] ;
        int cnt[MAXN] ;
        int num[MAXN] ;
        int len[MAXN] ;
        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 ) {
            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] ;
                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] ;
            
        }
    } str;
    
    int main()
    {
    	int t;
    	string a;
    	cin>>t;
    	int k=1;
    	while(t--)
    	{
    		cin>>a;
    		str.init();
    		int len=a.size();
    		for(int i=0;i<len;i++)
    		{
    			str.add(a[i]);
    		}
    		cout<<"Case #"<<k++<<": ";
    		cout<<str.p-2<<endl;
    	}
    	return 0;
     } 
    
  • 相关阅读:
    mysql允许远程访问
    ubuntu pip install MySQL-python mysql_config not found
    ubuntu 阿里云源
    V
    KMP算法之next函数解释(大量的反证法 和数学归纳法来袭)
    日常ACM题目
    F
    J
    中缀表达式求值 ,中缀表达转化为后缀表达式求值,
    数据结构
  • 原文地址:https://www.cnblogs.com/tianming1/p/11609871.html
Copyright © 2011-2022 走看看