字典树数组形式写法

第一题:

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

 Status

Description

Download as PDF

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 , 1$ \le$S$ \le$4000 .

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

 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,5104] 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
 

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值