UVa 11488 - Hyper Prefix Sets (Trie)

来源:互联网 发布:vs2015mfc编程入门教程 编辑:程序博客网 时间:2024/05/22 00:10

UVA - 11488

Hyper Prefix Sets
Time Limit: 2000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

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 : ManzururRahman Khan

Source

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 3. Data Structures :: String Algorithms ::Exercises: Beginner
Root :: Prominent Problemsetters :: Abdullah-al-Mahmud (Satej)

[Submit]   [Go Back]   [Status]  




题意:

给定一个字符串集合S,定义P(S)为所有字符串的公共前缀长度与S中字符串个数的乘积。

给定n个01字符串,从中选择一个集合S,是的P(S)最大。



Trie简单维护一下每个节点下有多少字符串就行了,然后插入的时候更新下答案即可




#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 1e-9;const double PI = (4.0*atan(1.0));const int maxn = 50000 + 20;const int maxnode = maxn * 200 + 20;int ch[maxnode][2];int val[maxnode];int sz;void init() {    sz = 1;    memset(ch[0], 0, sizeof(ch[0]));}int insert(char * s) {    int ret = 0;    int u = 0, n = strlen(s);    for(int i=0; i<n; i++) {        val[u]++;        ret = max(ret, i*val[u]);        int c = s[i] - '0';        if(!ch[u][c]) {            memset(ch[sz], 0, sizeof(ch[sz]));            val[sz] = 0;            ch[u][c] = sz++;        }        u = ch[u][c];    }    val[u]++;    ret = max(ret, val[u] * n);    return ret;}char str[maxn];int main() {    int T;    scanf("%d", &T);    while(T--) {        int n;        int ans = 0;        scanf("%d", &n);        init();        for(int i=0; i<n; i++) {            scanf("%s", str);            int ret = insert(str);            ans = max(ans, ret);        }        printf("%d\n", ans);    }    return 0;}




0 0