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 {
    Trie *next[2];
    int vis;
    Trie()   //这个方法开节点很方便的,码一下
    {
        for (int i = 0; i<2; i++)
            this->next[i] = NULL;
        this->vis = 0;
    }
};
void insertrie(Trie* rt, string s)
{
    int len = s.length();
    Trie *p = rt;
    for (int i = 0; i<len; i++)
    {
        int id = s[i] - ‘0‘;
        if (p->next[id] == NULL)
        {
            p->next[id] = new Trie;
        }
        p = p->next[id];
        p->vis++;
        ans = max(ans, (i+1)*p->vis);
    }
}
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        ans = 0;
        Trie *rt=new Trie;
        for (int i = 0; i<n; i++)
        {
            cin >> s;
            insertrie(rt, s);
        }
        cout << ans << endl;
    }
    return 0;
}

小笔记?(^?^*)

背景+定义:字典树进行推广实际上是一个N叉树,字典树相对比较简单,但重要的是在此基础上                       的AC自动机比较难。字典树的功能实际上是对于很多的串进行压缩。

板子:

emmmm闭馆了hahahha……明天补

原文地址:https://www.cnblogs.com/Egoist-/p/8361609.html

时间: 2024-11-28 15:56:14

UVA 11488 Hyper Prefix Sets 字典树的相关文章

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

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>

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 goodnes

UVA 11488(Hyper Prefix Sets-Trie统计)

Pre x goodness of a set string is length of longest common pre x*number of strings in the set. For example the pre x goodness of the set f000,001,0011g is 6.You are given a set of binary strings. Find the maximum pre x goodness among all possible sub

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

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

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

UVA 3942 -- Remember the Word (字典树+dp)

Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can't remember numbers clear

208. Implement Trie (Prefix Tree)字典树

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 在Trie树中主要有3个操作,插入.查找和删除.一般情况下Trie树中很少存在删除单独某个结点的情况,因此只考虑删除整棵树. 1.插入 假设存在字符串str,Trie树的根结点为root.i=0,p=root. 1)取str[