初涉trie

trie:字符串算法中的重要“数据结构”

什么是trie

trie就是利用字符串的公共前缀所建成的树。

众所周知树是有很多很好的性质的,于是trie可以结合其他知识点做一些有趣的事情。

trie的例题

注意

trie的题一般数组开成$f[lensSum][size]$,其中$lensSum$是所有字符串的长度之和,$size$是字符集大小。

【判断前缀】poj3630Phone List

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 catalogue listed these numbers:

  • Emergency 911
  • Alice 97 625 999
  • Bob 91 12 54 26

In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output "YES" if the list is consistent, or "NO" otherwise.

Sample Input

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES

题目分析

这个是trie最基础的应用——判断前缀。

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<cstring>
 4 const int maxn = 100035;
 5 const int maxe = 13;
 6 
 7 int tt,n,tot;
 8 int f[maxn][maxe];
 9 char ch[maxe];
10 bool vis[maxn],fl;
11 
12 int read()
13 {
14     char ch = getchar();
15     int num = 0;
16     bool fl = 0;
17     for (; !isdigit(ch); ch = getchar())
18         if (ch=='-') fl = 1;
19     for (; isdigit(ch); ch = getchar())
20         num = (num<<1)+(num<<3)+ch-48;
21     if (fl) num = -num;
22     return num;
23 }
24 void insert(char *s)
25 {
26     int n = strlen(s), rt = 1;
27     for (int i=0; i<n; i++)
28     {
29         int w = s[i]-'0';
30         if (!f[rt][w]) f[rt][w] = ++tot;
31         else if (i==n-1) fl = 1;
32         rt = f[rt][w];
33         if (vis[rt]) fl = 1;
34     }
35     vis[rt] = 1;
36 }
37 int main()
38 {
39     tt = read();
40     while (tt--)
41     {
42         memset(vis, 0, sizeof vis);
43         memset(f, 0, sizeof f);
44         fl = 0, tot = 1, n = read();
45         for (int i=1; i<=n; i++)
46         {
47             scanf("%s",ch);
48             insert(ch);
49         }
50         printf("%s
",!fl?"YES":"NO");
51     }
52     return 0;
53 }

【前缀统计】bzoj1590: [Usaco2008 Dec]Secret Message 秘密信息

Description

    贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.
    信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.
    对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.
    在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

Input

    第1行输入N和M,之后N行描述秘密信息,之后M行描述密码.每行先输入一个整数表示信息或密码的长度,之后输入这个信息或密码.所有数字之间都用空格隔开.

Output

    共M行,输出每条密码的匹配信息数.

题目分析

那么显然就是一个前缀计数。分成两类:其他是它前缀;它是其他前缀。
 1 #include<bits/stdc++.h>
 2 const int maxn = 10035;
 3 const int maxe = 2;
 4 const int maxNode = 500035;
 5 
 6 struct node
 7 {
 8     int end,pass;
 9 }a[maxNode];
10 int f[maxNode][maxe];
11 int n,m,lens,tot,r[maxn];
12 
13 int read()
14 {
15     char ch = getchar();
16     int num = 0;
17     bool fl = 0;
18     for (; !isdigit(ch); ch = getchar())
19         if (ch=='-') fl = 1;
20     for (; isdigit(ch); ch = getchar())
21         num = (num<<1)+(num<<3)+ch-48;
22     if (fl) num = -num;
23     return num;
24 }
25 void insert()
26 {
27     int u = 1;
28     for (int i=1; i<=lens; i++)
29     {
30         if (!f[u][r[i]]) f[u][r[i]] = ++tot;
31         u = f[u][r[i]];
32         a[u].pass++;
33     }
34     a[u].pass--, a[u].end++;
35 }
36 void query()
37 {
38     int cnt = 0, u = 1;
39     for (int i=1; i<=lens; i++)
40     {
41         u = f[u][r[i]];
42         if (!u) break;
43         cnt += a[u].end;
44     }
45     cnt += a[u].pass;
46     printf("%d
",cnt);
47 }
48 int main()
49 {
50     tot = 1, n = read(), m = read();
51     for (int i=1; i<=n; i++)
52     {
53         lens = read();
54         for (int j=1; j<=lens; j++) r[j] = read();
55         insert();
56     }
57     for (int i=1; i<=m; i++)
58     {
59         lens = read();
60         for (int j=1; j<=lens; j++) r[j] = read();
61         query();
62     }
63     return 0;
64 }
 

【xor最值】bzoj4260: Codechef REBXOR

Description

Input

输入数据的第一行包含一个整数N,表示数组中的元素个数。
第二行包含N个整数A1,A2,…,AN。

Output

输出一行包含给定表达式可能的最大值。

Sample Input

5
1 2 3 1 2

Sample Output

6

HINT

满足条件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。

对于100%的数据,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。

题目分析

这题就是要稍加建模的题了。

若用dp的思想:$l[i]$表示$1≤l≤r≤i$的最大异或和,$r[i]$表示$i≤l≤r≤n$的最大异或和,那么有$ans=max{l[i]+r[i+1]}$。

问题就在于求$l[i],r[i]$,这里讨论$l[i]$的求法,$r[i]$同理。若$r!=i$,那么$l[i]=l[i-1]$;否则就是固定了右端点,再找一个左端点使得$a[x]~a[i]$异或和最大。

粗看xor是没有前缀和加法性质的。但是这不就等于在一个集合里找一个数求其与给定数最大的异或和吗?这就转化成为trie的另一个经典应用了。

 1 #include<bits/stdc++.h>
 2 const int maxn = 400035;
 3 const int maxe = 3;
 4 
 5 int n,tot,cnt,ans;
 6 int f[maxn<<5][maxe],a[maxn];
 7 int l[maxn],r[maxn];
 8 
 9 int read()
10 {
11     char ch = getchar();
12     int num = 0;
13     bool fl = 0;
14     for (; !isdigit(ch); ch = getchar())
15         if (ch=='-') fl = 1;
16     for (; isdigit(ch); ch = getchar())
17         num = (num<<1)+(num<<3)+ch-48;
18     if (fl) num = -num;
19     return num;
20 }
21 void insert(int x)
22 {
23     int u = 1;
24     for (int i=1<<30; i; i>>=1)
25     {
26         int c = (x&i)?1:0;
27         if (!f[u][c]) f[u][c] = ++tot;
28         u = f[u][c];
29     }
30 }
31 int find(int x)
32 {
33     int u = 1, ret = 0;
34     for (int i=1<<30; i; i>>=1)
35     {
36         int c = (x&i)?0:1;
37         if (f[u][c])
38             ret += i, u = f[u][c];
39         else u = f[u][1-c];  //这里u打成c调了半小时……
40     }
41     return ret;
42 }
43 int main()
44 {
45     n = read();
46     for (int i=1; i<=n; i++) a[i] = read();
47     memset(f, 0, sizeof f);
48     tot = 1, cnt = 0, insert(0);
49     for (int i=1; i<=n; i++)
50     {
51         cnt ^= a[i];
52         insert(cnt);
53         l[i] = std::max(l[i-1], find(cnt));
54     }
55     memset(f, 0, sizeof f);
56     tot = 1, cnt = 0, insert(0);
57     for (int i=n; i>=1; i--)
58     {
59         cnt ^= a[i];
60         insert(cnt);
61         r[i] = std::max(r[i+1], find(cnt));
62     }
63     for (int i=1; i<n; i++)
64         ans = std::max(ans, l[i]+r[i+1]);
65     printf("%d
",ans);
66     return 0;
67 }

END

原文地址:https://www.cnblogs.com/antiquality/p/9325746.html