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

题目大意:

如果a表示公共前缀的长度,b表示含有这个前缀的字符串个数,问你a*b的最大值。

解题思路:

建立一棵Trie树,边建边查,直接更新 长度乘以个数的最大值

解题代码:

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

const int maxn=500000;
int tree[maxn][2];
int val[maxn],cnt;
int n,ans;

void insert(string st){
    int s=0;
    for(int i=0;i<st.length();i++){
        if( tree[s][st[i]-'0']==0 ) tree[s][st[i]-'0']=++cnt;
        s=tree[s][st[i]-'0'];
        val[s]++;
        if((i+1)*val[s]>ans) ans=(i+1)*val[s];
    }
}

void initial(){
    cnt=ans=0;
    memset(val,0,sizeof(val));
    memset(tree,0,sizeof(tree));
}

void solve(){
    cin>>n;
    for(int i=0;i<n;i++){
        string st;
        cin>>st;
        insert(st);
    }
    cout<<ans<<endl;
}

int main(){
    int t;
    cin>>t;
    while(t-- >0){
        initial();
        solve();
    }
    return 0;
}

HDU 11488 Hyper Prefix Sets (字符串-Trie树),布布扣,bubuko.com

时间: 2024-12-24 08:48:31

HDU 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

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

HDU 1251 统计难题 (字符串-Trie树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

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

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树很容易实现,然后插完之后,把每个节点遍历一遍

HDU 3499 分层图最短路+Trie树

点击打开链接 题意:给n个城市和m条无向边,然后给了起点和终点,然后你有一次机会使得其中的一张票价减半,问最小花费是多少 思路:明显是裸的分层图嘛,而且层数就只为2比较简单,但是注意的是城市的名字之类的,我用的Trie树来处理的,RE了几次,每组过后将Trie树释放就好了,然后注意那个减半的价钱是直接/2,WA了几次加了1除以的2,o(︶︿︶)o 唉 #include <queue> #include <vector> #include <stdio.h> #inclu

杭电 HDU 1247 ACMHat’s Words(trie树 或着STL)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9620    Accepted Submission(s): 3438 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl