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


Problem Setter : Abdullah Al Mahmud

Special Thanks : Manzurur Rahman Khan

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

const int maxn = 5000000;
vector<string> vs;
int cnt,ans,n;
int ch[maxn][2];
int val[maxn];

inline int getIdx(char a){
    return a-'0';
}
void insert(string st){
    int u = 0;
    for(int i = 0; i < st.size(); i++){
        int k = getIdx(st[i]);
        if(!ch[u][k]){
            memset(ch[cnt],0,sizeof ch[cnt]);
            val[cnt]++;
            ans = max(ans,val[cnt]*(i+1));
            ch[u][k] = cnt++;
        }else{
            val[ch[u][k]]++;
            ans = max(ans,val[ch[u][k]]*(i+1));
        }
        u = ch[u][k];
    }
}
int main(){

    int ncase;
    cin >> ncase;
    while(ncase--){
        vs.clear();
        memset(val,0,sizeof val);
        memset(ch[0],0,sizeof ch[0]);
        cnt = 1;
        ans = 0;
        cin >> n;
        while(n--){
            string st;
            cin >> st;
            insert(st);
        }
        cout<<ans<<endl;
    }
    return 0;
}

UVa11488-Hyper Prefix Sets(trie树)

时间: 2024-08-02 22:52:29

UVa11488-Hyper Prefix Sets(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 字典树

模板题,字典树最基本的操作 在看别人的板子的时候学到了一点小技巧 下面贴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 {

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(字典树)

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

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 11488 Hyper Prefix Sets

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

Trie树模板

struct Trie{ int ch[maxnnode][sigma_size]; int val[maxnnode]; int sz; Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));} int idx(char c) {return c-'a';} void Insert(char *s,int v){ int u=0,n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]){

LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)

题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! 1 class TrieNode { 2 public: 3 TrieNode* chd[26]; 4 bool flag; 5 // Initialize your data structure here. 6 TrieNode() { 7 memset(chd,0,sizeof(chd)); 8 flag=0; 9 } 10 11 }; 12 13 class Trie { 14 public: 15 Trie() {

笔试算法题(39):Trie树(Trie Tree or Prefix Tree)

出题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All the descendants of a node have a common prefix of the sequence associated with that node, and the root is associated with the empty sequence. 由于不同的se