字典树数组形式写法

第一题:

Remember the Word

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

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 clearly, he just uses sticks to help himself. Allowing for Jiejie‘s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of
ways the given word can be divided, using the words in the set.

Input

The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

The second line contains an integer S , 1S4000 .

Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words
will be lowercase.

There is a blank line between consecutive test cases.

You should proceed to the end of file.

Output

For each test case, output the number, as described above, from the task description modulo 20071027.

Sample Input

abcd
4
a
b
cd
ab

Sample Output

Case 1: 2

Submit Status

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define maxn 400000 + 10
#define M 300000 + 10
#define mod 20071027

int tree[maxn][27], val[maxn], top;
int d[maxn];
char s[M], tmp[105];

void init()
{
    top = 1;
    memset(tree[0], 0, sizeof tree[0]);
    memset(d, 0, sizeof d);
}

void insert(char *s, int v)
{
    int len = strlen(s), u = 0;
    for(int i = 0; i < len; i++)
    {
        int c = s[i] - 'a' ;
        if(!tree[u][c])
        {
            memset(tree[top], 0, sizeof tree[top]);
            val[top] = 0;
            tree[u][c] = top++;
        }
        u = tree[u][c];
    }
    val[u] = v;
}

int len ;

int query(char *s, int pos)
{
    int u = 0;
    int res = 0;
    for(int i = pos; i < len; i++)
    {
        int c = s[i] - 'a' ;
        u = tree[u][c];
        if(!u) return res;
        if(val[u])
        {
            res = (res + d[i + 1] ) % mod ;
        }
    }
    return res;
}

int main()
{
    int kase = 0;
    while(~scanf("%s", s))
    {
        init();
        len = strlen(s);
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%s", tmp);
            insert(tmp, 1);
        }
        d[len] = 1;
        for(int i = len - 1; i >= 0; i--)
        {
            d[i] = query(s, i);
        }
        printf("Case %d: %d\n", ++kase, d[0]);
    }
    return 0;
}

第二题:

ZYB loves Xor I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 734    Accepted Submission(s): 320

Problem Description

Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj)) (i,j∈[1,n])

We define that lowbit(x)=2k,k
is the smallest integer satisfied ((x and 2k)>0)

Specially,lowbit(0)=0

Because the ans may be too big.You just need to output ans mod
998244353

Input

Multiple test cases, the first line contains an integer T(no more than 10), indicating the number of cases. Each test case contains two lines

The first line has an integer n

The second line has n integers A1,A2....An

n∈[1,5?104],Ai∈[0,229]

Output

For each case, the output should occupies exactly one line. The output format is Case #x: ans, here x is the data number begins at 1.

Sample Input

2
5
4 0 2 7 0
5
2 6 5 4 0

Sample Output

Case #1: 36
Case #2: 40

Source

BestCoder Round #44

Recommend

hujie   |   We have carefully selected several similar problems for you:  5352 5351 5350 5348 5347

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MOD 998244353
#define M 2000010
using namespace std;

int top;// top 表示节点个数
int tree[M][2]; // 字典树
int val[M]; //字符串的权值 当val[i] 大于0时i是单词节点

void init()
{
    top=1;
    //memset(tree, 0, sizeof tree);
    memset(tree[0], 0, sizeof tree[0]);
    memset(val, 0, sizeof val);
}

void insert(int v)
{
    int u = 0;//根节点
    for(int i = 0; i < 30 ; i++)
    {
        int t = v & ( 1<< i );
        if(t) t = 1;
        if(!tree[u][t])
        {
            memset(tree[top], 0, sizeof tree[top]);
            val[top] = 1;
            tree[u][t] = top++;
        }
        else
        {
            val[tree[u][t]]++;
        }
        u = tree[u][t];
    }
}

int ans;
void find(int v)
{
    int u = 0;
    for(int i = 0; i < 30; i++)
    {
        int t = v & (1 << i);
        if(t) t = 1;
        if(tree[u][t ^ 1])
        ans = ( ans + (val[tree[u][t ^ 1]]) * (1 << i)) % MOD;
        u = tree[u][t] ;
    }
}

int a[M];

int main()
{
   int T, n;
   scanf("%d", &T);
   int kase = 0;
   while(T--)
   {
       init();
       ans = 0;
       scanf("%d", &n);
       for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
            insert(a[i]);
        }
       for(int i=0; i<n; i++)
        find(a[i]);
       printf("Case #%d: %d\n", ++kase, ans);
   }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-17 15:07:54

字典树数组形式写法的相关文章

HDU 4287 Intelligent IME(字典树数组版)

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4776    Accepted Submission(s): 2227 Problem Description We all use cell phone today. And we must be familiar with the intelligen

字典树数组模板

自己实在太懒了,就没有自己再写一份代码,这是另一个博客上直接找来的,它的数组模板写的挺好的,指针模板一般般吧!~~ http://blog.csdn.net/king_cannon_fodder/article/details/77175620 ///这里以输入字符串后,查询字符串出现的次数为例 #include <bits/stdc++.h> #define maxn 1000000 using namespace std; struct Trie { int next[26];///表示下

字典树(数组 查找前缀)

int trie[400001][26],len,root,tot,sum[400001]; bool p; int n,m; char s[11]; void insert() { len=strlen(s); root=0; for(int i=0;i<len;i++) { int id=s[i]-'a'; if(!trie[root][id]) trie[root][id]=++tot; sum[trie[root][id]]++;//前缀后移一个位置保存 root=trie[root][

hdu 4825 Xor Sum(字典树)

Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧么? Input 输入包含若干组测试数据,每组测试数据

HDU 4825 Xor Sum(二进制的字典树,数组模拟)

题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int node[3011111][2]; int tag,m,n,cas=0,T; long long one[64],allone,tmp; //0/1树的加数据操作,增加一个32的数 //其中如果当前位是0,则加左儿子,

IOS数组、字典、NSNumber 新写法—— @[]、@{}

IOS数组.字典.NSNumber 新写法—— @[].@{} //标准写法 NSNumber * number = [NSNumber numberWithInt:1]; NSArray * array = [NSArray arrayWithObjects:@"one", @"two", nil]; NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1&quo

优先队列 + 并查集 + 字典树 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 1 struct node 2 { 3 int x,y,step; 4 friend bool operator <(node s1,node s2) // 定义结构体 的时候 这个 就是 用于 优先队列的 基准排序 5 { 6 return s1.step>s2.step; // 步数小的 在上 为小顶堆 7 } 8 }; 9 priority_queue<node>Q; // 优先队列的结构

Codeforces Round #470 (Div 2) B 数学 C 二分+树状数组 D 字典树

Codeforces Round #470 B. Primal Sport 数学题,对 x2 和 x1 分解质因子即可. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b;

白话算法与数据结构之【字典树】

1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高.      Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子