您的位置:首页 > 其它

uva 11488 - Hyper Prefix Sets(字典树)

2014-06-20 20:23 961 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: