BZOJ1174 [Balkan2007] Toponyms 鄰接鏈表優化 TRIE樹

大家都吼強,可與之共勉嗯嗯。

題意:
  給您一個字符集合,你從其中找出一些字符串出來。 希望你找出來的這些字符串的最長公共前綴× 字符串的總個數最大化。

  空格也是嗯,所以我看不懂樣例!!!!!!!!

題解:
  傻逼題,哎呀MLE了。怎麼辦,那就暴力找轉移叭!!!
  我們發現轉移go[cur][26] 並不是26 個都用上了(又不是補全AC自動機),於是我萌就可以用鏈表存轉移邊。
  A掉惹!!!

/**************************************************************
    Problem: 1174
    User: Lazer2001
    Language: C++
    Result: Accepted
    Time:4716 ms
    Memory:98944 kb
****************************************************************/

# include <bits/stdc++.h>

# define N 5000010

struct edge  {
    char c ; int to, nxt ;  
} g [N] ;

int cnt [N], ncnt ;
int head [N], ecnt ;

# undef N

long long ans ( 0 ) ;

inline void insert ( )  {
    int cur ( 0 ) ;
    register int c ;
    while ( isspace ( c = getchar ( ) ) ) ;
    for ( int i = 1 ; c != '\n' ; ++ i, c = getchar ( ) )  {
        int nxt = -1 ;
        for ( int it = head [cur] ; it ; it = g [it].nxt )
            if ( g [it].c == c )  {
                nxt = g [it].to ;
                break ;
            }
        if ( nxt == -1 )  {
            ++ ecnt ;
            g [ecnt].c = c, g [ecnt].to = ( nxt = ++ ncnt ), g [ecnt].nxt = head [cur] ;  head [cur] = ecnt ;
        }
        cur = nxt ;
        ++ cnt [cur] ;
        ans = std :: max ( ans, 1LL * ( cnt [cur] ) * i ) ;
    }
}

int main ( )  {
    int n ;
    scanf ( "%d", & n ) ;
    while ( n -- )  {
        insert ( ) ;
    }
    printf ( "%lld\n", ans ) ;
    return 0 ;
}
發佈了223 篇原創文章 · 獲贊 38 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章