UVA - 11488 字典树

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483

题意:给定一堆由0,1组成的串,现在要求一个最大值,值的结果为:串前缀*用于此前缀的字符串个数

思路:字典树,在插入字符串的时候就开始统计,对于插入的每个字符串的前缀的值都累加,然后一边插入一边维护最大值即可。

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <time.h>
using namespace std;
typedef long long int LL;
const int MAXN = 50000 * 200 + 5;
char str[50];
struct Trie{
    int val;
    int child[2];
    Trie(){
        val = 0;
        memset(child, 0, sizeof(child));
    }
}trie[MAXN];
int trieN, ans;
void Insert(char *str){
    int d, x = 0;
    for (int i = 0; str[i]; i++){
        d = str[i] - ‘0‘;
        if (trie[x].child[d] == 0){
            trie[x].child[d] = ++trieN;
        }
        x = trie[x].child[d];
        trie[x].val++;
        ans = max(ans, (i + 1)*trie[x].val);
    }
}
void Delete(int u){
    for (int i = 0; i < 2; i++){
        if (trie[u].child[i]){
            Delete(trie[u].child[i]);
        }
    }
    trie[u].val = 0;
    memset(trie[u].child, 0, sizeof(trie[u].child));
}
int main(){
    int t, n;
    scanf("%d", &t);
    while (t--){
        scanf("%d", &n); trieN = 0, ans = 0;
        for (int i = 1; i <= n; i++){
            scanf("%s", str);
            Insert(str);
        }
        printf("%d\n", ans);
        Delete(0);
    }
    return 0;
}
时间: 2024-08-10 19:10:10

UVA - 11488 字典树的相关文章

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

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

题目链接:uva 1385 - Billing Tables 题目大意:给定n个电话前缀,每个前缀是一个区域的前缀,现在要生成一个新的电话单,即对于每个电话号码,从旧的电话单上从前向后遍历,如果出现前缀匹配,则该电话号码对应的即为当前的区号,要求生成的新电话单尽量小. 解题思路:用dfs建立字典树,在区间范围内的点对应均为对应的区号,注意如果70.71.72....79都为SB的话,那么可以合并成7,并且对应区号为SB. 注意合并的条件为区号相同即可,并不是说对应旧电话单匹配位置相同. 注意这组

uva 1556 - Disk Tree(字典树)

题目连接:uva 1556 - Disk Tree 题目大意:给出N个目录关系,然后按照字典序输出整个文件目录. 解题思路:以每个目录名作为字符建立一个字典树即可,每个节点的关系可以用map优化. #include <cstdio> #include <cstring> #include <map> #include <string> #include <iostream> #include <algorithm> using nam

uva 11732 - strcmp() Anyone?(字典树)

题目链接:uva 11732 - strcmp() Anyone? 题目大意:给定n个串,然后两两之间比较,问说总共要比较多少次. 解题思路:字典树,建立出字典树,然后根据字典树的性质在节点记录有多少个字符串包含该节点.因为节点的个数比较多,所以用左孩子右兄弟的方法建立字典树. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long

UVA 11732 strcmp() Anyone? (压缩版字典树)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832 按照正常的字典树建树会MLE 所以需要采用树的压缩算法来建树 #include <cstdio> #include <iostream> #include <cstring> #define maxn 4000010 #define ma

uva 1462 - Fuzzy Google Suggest(字典树+dfs)

题目链接:uva 1462 - Fuzzy Google Suggest 题目大意:模拟google的模糊搜索,给定给一个字符串集合,然后有n次搜索,每次有一个整数x和一个字符串,表示可以对字符串进行x次修改,包括增加.修改和删除一个字符,问修改后的字符可能是字符集中有多少个字符串的前缀. 解题思路:先建立字典树,对于每次搜索,在字典树上进行dfs,根据参数x和字符串匹配的位置进行处理,对于匹配到末尾的位置标记为2,然后对于第二次dfs,搜索每个分支上最早出现2的位置即可. #include <

UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 5228 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨论下怎么得到fib数的前40位. 首先,因为没可能每一项的fib都求出来的了.空间都存不下来.所以,只能够缩小规模,有个明显的道理,用每个fib的前60项相加取前40即可.为什么呢?因为没有后效性,后面的项相加不会影响到前40项.就是你有40--60这些项来缓冲就够了,这些才是主要的进位项.后面的相