题解 UVA11488 【Hyper Prefix Sets】

Topic Link
First, we enter multiple strings of data, as well as seeking common prefix, it is easy to think of this question is \ (Trie \) applications

We \ (Trie \) tree built and ran in a tree above \ (dp \) can be.

Namely \ (dp (u, dis) \) represented by \ (u \) is the root of the subtree, \ (u \) depth \ (dis \) answer when

Defined by the subject can easily think, \ (DP (U, DIS) = max \ {DIS TOT * [U], DP (V, + DIS. 1) \} \)

\ (tot [u] \) is to \ (U \) the number of leaf nodes of root subtree (i.e. the root node to \ (U \) period for the common prefix, this string having a common prefix an amount of \ (TOT [U] \) )

So we can expect a primary codes:

#include <bits/stdc++.h>
using namespace std;
struct Trie{
    static const int maxnode = 100100;
    static const int sigma_size = 2;
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    inline void init(){sz = 1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}
    Trie(){sz = 1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}
    inline int idx(char c){return c - '0';}
    inline void insert(const string &s){
        int u = 0,n = s.size();
        for(int i = 0;i < n;i++){
            int c = idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;        
            }
            u = ch[u][c];
        }
        val[u]++;
    }
    inline int tot(int u){
        int ret = val[u];
        for(int c = 0;c < sigma_size;c++)
            if(ch[u][c])ret += tot(ch[u][c]);
        return ret;
    }
    inline int dfs(int u,int dis){
        int ret = dis * tot(u);
        for(int c = 0;c < sigma_size;c++)
            if(ch[u][c])ret = max(ret,dfs(ch[u][c],dis + 1));
        return ret;
    }
}tt;
string ss;
int T,n;
inline void solve(){
    tt.init();      
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> ss;
        tt.insert(ss);
    }
    printf("%d\n",tt.dfs(0,0));
}
int main(){
    ios::sync_with_stdio(false);
    cin >> T;
    while(T--)
        solve();
    return 0;
}

The code that ran 160 ms , will not time out, but we still consider optimizing.

Every \ (dp \) is considered a \ (TOT \) , time complexity explosion in situ (Although the data harmless H2O)。

then

We can build \ (Trie \) tree when the \ (tot \) array to figure out.

Usually built \ (Trie \) tree leaf node is to empower value, in this question, every weight we put a point over which the achievements are \ (+ 1 \) , this right is to the value of each node the number of the leaf node to the root node of the subtree

Code:

#include <bits/stdc++.h>//懒得打了,大家最好别用万能头文件
using namespace std;
struct Trie{
    static const int maxnode = 100100;//最大节点数
    static const int sigma_size = 2;//字符集大小
    int ch[maxnode][sigma_size];//儿子数组
    int val[maxnode];//节点附加权值,即转移方程的tot数组
    int sz;//当前节点总数
    inline void init(){sz = 1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}//初始化函数
    Trie(){sz = 1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}
    inline int idx(char c){return c - '0';}
    inline void insert(const string &s){//插入字符串,传引用避免拷贝开销
        int u = 0,n = s.size();
        for(int i = 0;i < n;i++){
            int c = idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;        
            }
            u = ch[u][c];
            val[u]++;//这里即上文所说的统计子树节点个数
        }
    }
    inline int dfs(int u,int dis){//最终答案
        int ret = val[u] * dis;
        for(int c = 0;c < sigma_size;c++)
            if(ch[u][c])
                ret = max(ret,dfs(ch[u][c],dis + 1));
        return ret;
    }
}tt;
string ss;
int T,n;
inline void solve(){//多组数据求解
    tt.init(); 
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> ss;
        tt.insert(ss);
    }
    printf("%d\n",tt.dfs(0,0));
}
int main(){
    ios::sync_with_stdio(false);
    cin >> T;
    while(T--)
        solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/colazcy/p/11514703.html