zoukankan      html  css  js  c++  java
  • uva 11488

    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
    " , ans);
    }
    
    int main(){
        int t;
        scanf("%d" , &t);
        while(t--){
            initial();
            readcase();
        }
        return 0;
    }
    


  • 相关阅读:
    HAProxy、Keepalived 在 Ocatvia 的应用实现与分析
    Octavia 的 HTTPS 与自建、签发 CA 证书
    Octavia 创建 loadbalancer 的实现与分析
    OpenStack Rally 质量评估与自动化测试利器
    自建 CA 中心并签发 CA 证书
    Failed building wheel for netifaces
    通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
    OpenStack Placement Project
    我们建了一个 Golang 硬核技术交流群(内含视频福利)
    没有图形界面的软件有什么用?
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6875608.html
Copyright © 2011-2022 走看看