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 goodness among all possible subsets
of these binary strings.

Input

First line of the input contains T(≤20) the number of test cases. Each of the test cases start with n(≤50000) the number of strings. Each of the next n lines contains a string containing only 0 and 1. Maximum length of each of these string is 200.

Output

For each test case output the maximum prefix goodness among all possible subsets of n binary strings.

Sample Input                             Output for Sample Input


4

4

0000

0001

10101

010

2

01010010101010101010

11010010101010101010

3

010101010101000010001010

010101010101000010001000

010101010101000010001010

5

01010101010100001010010010100101

01010101010100001010011010101010

00001010101010110101

0001010101011010101

00010101010101001

 


6

20

66

44

字典树,每条边记录一下深度。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
using namespace std;

struct edge{
    int u , v , letter , sum , dep;
    edge(int a = 0 , int b = 0 , int c = 0 , int d = 0 , int f = 0){
        u = a , v = b , letter = c , sum = d , dep = f;
    }
};
vector<edge> e;
vector<int> head , next;
int ans;

void add(int  u , int v , int letter , int sum , int dep){
    e.push_back(edge(u , v , letter , sum , dep));
    next.push_back(head[u]);
    head[u] = next.size()-1;
    head.push_back(-1);
}

void addLibrary(string s){
    int u = 0 , index = 0;
    while(index < s.length()){
        int Next = head[u];
        while(Next != -1){
            if(e[Next].letter == s[index]-'0') break;
            Next = next[Next];
        }
        if(Next == -1) break;
        e[Next].sum++;
        index++;
        u = e[Next].v;
    }
    while(index < s.length()){
        add(u , head.size() , s[index]-'0' , 1 , index+1);
        index++;
        u = head.size()-1;
    }
}
void initial(){
    e.clear();
    next.clear();
    head.clear();
    head.push_back(-1);
    ans = 0;
}

void readcase(){
    string s;
    int n;
    scanf("%d" , &n);
    getchar();
    while(n--){
        cin >> s;
        //cout <<"+"<<s<<endl;
        addLibrary(s);
    }
    for(int i = 0; i < e.size(); i++){
        if(e[i].sum*e[i].dep > ans) ans = e[i].sum*e[i].dep;
    }
    printf("%d\n" , ans);
}

int main(){
    int t;
    scanf("%d" , &t);
    while(t--){
        initial();
        readcase();
    }
    return 0;
}

uva 11488 - Hyper Prefix Sets(字典树),布布扣,bubuko.com

时间: 2024-10-19 18:04:38

uva 11488 - Hyper Prefix Sets(字典树)的相关文章

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 {

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>

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统计)

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

Hyper Prefix Sets

uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2483 题意:给你n个串,对于一个前缀,如果出现k次,就会得到前缀的长度*k,现在让你求长度*k的最大值. 题解:用trie树来搞.把每个串插入到trie中,记录每个子串出现的次数以及长度,trie树很容易实现,然后插完之后,把每个节点遍历一遍

uva 1462 - Fuzzy Google Suggest(字典树+dfs)

题目链接:uva 1462 - Fuzzy Google Suggest 题目大意:模拟google的模糊搜索,给定给一个字符串集合,然后有n次搜索,每次有一个整数x和一个字符串,表示可以对字符串进行x次修改,包括增加.修改和删除一个字符,问修改后的字符可能是字符集中有多少个字符串的前缀. 解题思路:先建立字典树,对于每次搜索,在字典树上进行dfs,根据参数x和字符串匹配的位置进行处理,对于匹配到末尾的位置标记为2,然后对于第二次dfs,搜索每个分支上最早出现2的位置即可. #include <

UVA 3942 -- Remember the Word (字典树+dp)

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 numbers clear

208. Implement Trie (Prefix Tree)字典树

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 在Trie树中主要有3个操作,插入.查找和删除.一般情况下Trie树中很少存在删除单独某个结点的情况,因此只考虑删除整棵树. 1.插入 假设存在字符串str,Trie树的根结点为root.i=0,p=root. 1)取str[