Tire 字典树&& uva1401

题意:给一个长字符串s(1<|s|<300000),和n(n<4000)个单词,求出有多少种把s分成单词的方法(白书P209)。

打模板!

#include<cstdio>
#include<cstring>

#define MOD 20071027
#define maxnode 4010*101
#define MAXLEN 300005
#define sigema_size 26

int ca=0,S,d[MAXLEN];
char str[MAXLEN],tmp[105];

struct Trie
{
    int sz;
    int ch[maxnode][sigema_size];
    int val[maxnode];

    void init() {sz=1;memset(ch,0,sizeof(ch));memset(val,0,sizeof(val));}
    int idx(char c) {return c-‘a‘;}
    void insert(char *s)
    {
        int u=0;
        for(int i=0;s[i];++i)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[c]));
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=1;
    }
    int find(char *s,int a)
    {
        int u=0,ans=0;
        for(int i=a;s[i];++i)
        {
            int c=idx(s[i]);
            if(!ch[u][c]) return ans;
            u=ch[u][c];
            if(val[u]) ans=(ans+d[i+1])%MOD;
        }
        return ans;
    }
}trie;
void solve()
{
    int n=strlen(str);
    d[n]=1;
    for(int i=n-1;i>=0;--i) d[i]=trie.find(str,i);
    printf("Case %d: %d\n",++ca,d[0]);
}
int main()
{
    while(~scanf("%s",&str))
    {
        trie.init();
        scanf("%d",&S);
        for(int i=0;i<S;++i){scanf("%s",tmp);trie.insert(tmp);}
        solve();
    }
    return 0;
}

Tire 字典树&& uva1401,布布扣,bubuko.com

时间: 2024-08-26 09:02:34

Tire 字典树&& uva1401的相关文章

The XOR Largest Pair(Tire字典树应用)

题目链接:传送门 思路:建立一个32位的字典树,对每一个要插入的数字查找它异或的最大值(就是尽量全部二进制的值都相反), 然后获得两个数异或的最大值. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e5+10; int ch[maxn*32][2],tot; void Insert(int x) { int i,u=1,c; for

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树

题目链接:D. Kuro and GCD and XOR and SUM 题意:两种操作:第一种给数组添加一个数,第二种输入x,k,s,要求从数组中找到一个数v,要求k能整除gcd(k,v);并且v<=s-x,然后异或v与k的异或值最大. 题解:对与k大于1的情况我们暴力枚举过去,k为1的特殊处理建一颗字典树,如果可以的满足条件的话,每次取值时往相反方向取. 1 #include<bits/stdc++.h> 2 #include <iostream> 3 #include

Uva1401(字典树)

1401 - Remember the Word Time limit: 3.000 seconds 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 Ji

Tire树(字典树)

from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交叉. 定义 在计算机科学中,trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有

poj 3764 The xor-longest Path(字典树)

题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值,找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每个节点到根节点路径的亦或和rec,那么任意路径均可以表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大,那么就建立字典树查询即可. #include <cstdio> #include <cstring> #include <algorithm> us

跳跃表,字典树(单词查找树,Trie树),后缀树,KMP算法,AC 自动机相关算法原理详细汇总

第一部分:跳跃表 本文将总结一种数据结构:跳跃表.前半部分跳跃表性质和操作的介绍直接摘自<让算法的效率跳起来--浅谈"跳跃表"的相关操作及其应用>上海市华东师范大学第二附属中学 魏冉.之后将附上跳跃表的源代码,以及本人对其的了解.难免有错误之处,希望指正,共同进步.谢谢. 跳跃表(Skip List)是1987年才诞生的一种崭新的数据结构,它在进行查找.插入.删除等操作时的期望时间复杂度均为O(logn),有着近乎替代平衡树的本领.而且最重要的一点,就是它的编程复杂度较同类

uva 1556 - Disk Tree(字典树)

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

Algorithms(字典树)

字典树 #ifndef TIRE_H_INCLUDED #define TIRE_H_INCLUDED /* ** 字典树 */ #define MAX 26 typedef struct Node { int num; struct Node* next[MAX]; }Tire; /* ** 创建一个节点 */ Tire* create(void); /* ** 插入一个字符串 */ Tire* insert(char str[], Tire* head); /* ** 搜索字符串 */ in

关于异或_字典树的一些性质

异或的性质 1. a ⊕ a = 0 2. a ⊕ b = b ⊕ a 3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c; 4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c. 5. a ⊕ b ⊕ a = b.     自反性 6.若x是二进制数0101,y是二进制数1011: 则x⊕y=1110 只有在两个比较的位不同时其结果是1,否则结果为0 即"两个输入相同时为0,不同则为1"! 字典树:又称为Trie,是一种用于快速检索的多叉