UVA 11488-Hyper Prefix Sets(Trie)

题意:

给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值

分析:

val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树求出最大值
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int>P;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 10000010
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
struct Trie{
    int ch[N][3];
    int val[N],num;
    void init(){
        num=1;
        memset(ch[0],0,sizeof(ch[0]));
        memset(val,0,sizeof(val));
    }
    //建树
    void build(char *s){
        int u=0,len=strlen(s);
        for(int i=0;i<len;++i){
            int v=s[i]-‘0‘;
            if(!ch[u][v]){
                memset(ch[num],0,sizeof(ch[num]));
                ch[u][v]=num++;
            }
            u=ch[u][v];
            val[u]++;
        }
    }
     //bfs
    ll query(){
        queue<P>q;
        ll maxv=0;
        q.push(P(0,0));
        while(!q.empty()){
            P p=q.front();
            q.pop();
            int u=p.first;
            int h=p.second;
            maxv=max(maxv,1LL*val[u]*h);
            for(int i=0;i<2;++i){
                if(ch[u][i]){
                    q.push(P(ch[u][i],h+1));
                }
            }
        }
       return maxv;
    }
}trie;
int main()
{
    int t,n;
    char str[310];
    scanf("%d",&t);
    for(int o=0;o<t;++o){
        trie.init();
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%s",str);
            trie.build(str);
        }
        printf("%lld\n",trie.query());
    }
return 0;
}    
 
时间: 2024-10-07 04:28:31

UVA 11488-Hyper Prefix Sets(Trie)的相关文章

uva 11488 - Hyper Prefix Sets(字典树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

HDU 11488 Hyper Prefix Sets (字符串-Trie树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

UVA 11488 Hyper Prefix Sets (Trie)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483 Hyper Prefix Sets Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodnes

UVa 11488 Hyper Prefix Sets

方法:Trie 本题其实就是trie的实现,每个节点需要记录两个值,深度 和 visit的次数,答案便是 max(深度 * visit的次数). 数组实现code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack>

UVA 11488 Hyper Prefix Sets 字典树

模板题,字典树最基本的操作 在看别人的板子的时候学到了一点小技巧 下面贴AC代码,顺便补一补字典树相关,顺便放一下橙子讲课的笔记 Trie三兄弟--标准Trie.压缩Trie.后缀Trie 字符串模式匹配算法--BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 #include<bits/stdc++.h> using namespace std; const int MAX = 5e4 + 5; string s; int n, t, ans; struct Trie {

UVa11488-Hyper Prefix Sets(trie树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

UVA 3942 - Remember the Word (Trie)

3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can't remember number

UVa 11488 超级前缀集合(Trie的应用)

https://vjudge.net/problem/UVA-11488 题意: 给定一个字符串集合S,定义P(s)为所有字符串的公共前缀长度与S中字符串个数的乘积.比如P( {000, 001, 0011} ) = 6.给n个01串,从中选择一个集合S,使得P(S)最大. 思路: 建立字典树,边插入边统计答案即可. 用两个变量分别记录前缀数量和前缀长度,每次插入时动态更新两者乘积. 1 #include<iostream> 2 #include<cstdio> 3 #includ

UVA 11488(Hyper Prefix Sets-Trie统计)

Pre x goodness of a set string is length of longest common pre x*number of strings in the set. For example the pre x goodness of the set f000,001,0011g is 6.You are given a set of binary strings. Find the maximum pre x goodness among all possible sub