https://nanti.jisuanke.com/t/17122

Barty have a computer, it can do these two things.

  1. Add a new string to its memory, the length of this string is even.

  2. For given 44 strings a,b,c,da,b,c,d, find out how many strings that can be product by a+s1+b+c+s2+da+s1+b+c+s2+d, and |a| + |s1| + |b| = |c| + |s2| + |d|∣a∣+∣s1∣+∣b∣=∣c∣+∣s2∣+∣d∣. |s|∣s∣ means the length of string ss, s1s1 and s2s2 can be any string, including "".

Please help your computer to do these things.

Input Format

Test cases begins with T(T \le 5)T(T≤5).

Then TT test cases follows.

Each test case begins with an integer Q(Q \le 30000)Q(Q≤30000).

Then QQ lines,

1 s: add a new string ss to its memory.

2 a b c d: find how many strings satisfying the requirement above.

\sum |s| + |a| + |b| + |c| + |d| \le 2000000∑∣s∣+∣a∣+∣b∣+∣c∣+∣d∣≤2000000.

Output Format

For type 22 query. Output the answer in one line.

样例输入

1
10
1 abcqaq
1 abcabcqaqqaq
2 ab bc qa aq
2 a c q q
1 abcabcqaqqwq
2 ab bc qa aq
2 a c q q
1 abcq
2 a c q q
2 a b c q

样例输出

1
2
1
3
3
1

题目来源

2017 ACM-ICPC 亚洲区(西安赛区)网络赛

首先对于每一个主串,都把它对半砍,前缀加入字典树0,前缀逆序加入字典树1,后缀加入字典树2,后缀逆序加入字典树3

所以每一个节点都开一个vector存着有哪些主串能经过这个节点。内存复杂度玄学

然后每次询问,用a去字典树0找,就能知道有哪些主串能匹配a

同理b、c、d,然后求一个交集即可。

这样会有些不合法

比如就是会使得

ab  bc这样,结合成abc

所以要用长度来判断是否合法,

lenstr(a) + lenstr(b)不能大于lenstr (s) / 2

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int N = , maxn = 2e6 + ;
struct Node {
vector<int> vc;
struct Node * pNext[N];
} tree[][ + ];
int t[];
int len[maxn];
char str[maxn];
char a[maxn], b[maxn], c[maxn], d[maxn];
struct Node * create(int id) {
struct Node * p = &tree[id][t[id]++];
p->vc.clear();
for (int i = ; i < N; ++i) p->pNext[i] = NULL;
return p;
}
void toInset(struct Node **T, char str[], int be, int en, int flag, int id, int which) {
struct Node *p = *T;
if (p == NULL) {
p = *T = create(id);
}
if (flag == -) {
for (int i = en; i >= be; --i) {
int tid = str[i] - 'a';
if (!p->pNext[tid]) p->pNext[tid] = create(id);
p = p->pNext[tid];
p->vc.push_back(which);
}
} else {
for (int i = be; i <= en; ++i) {
int tid = str[i] - 'a';
if (!p->pNext[tid]) {
p->pNext[tid] = create(id);
}
p = p->pNext[tid];
p->vc.push_back(which);
}
}
}
int vis[][maxn], DFN;
bool flag;
void ask(struct Node *T, char str[], int be, int en, int flag, int which) {
struct Node *p = T;
if (p == NULL) {
flag = true;
return;
}
if (flag == -) {
for (int i = en; i >= be; --i) {
int id = str[i] - 'a';
if (!p->pNext[id]) {
flag = true;
return ;
}
p = p->pNext[id];
}
for (int i = ; i < p->vc.size(); ++i) {
vis[which][p->vc[i]] = DFN;
}
} else {
for (int i = be; i <= en; ++i) {
int id = str[i] - 'a';
if (!p->pNext[id]) {
flag = true;
return ;
}
p = p->pNext[id];
}
// printf("fff");
// printf("%d * * ** \n", p->vc.size());
for (int i = ; i < p->vc.size(); ++i) {
vis[which][p->vc[i]] = DFN;
}
}
}
void work() {
t[] = t[] = t[] = t[] = ;
struct Node *T[];
for (int i = ; i < ; ++i) T[i] = NULL;
int q;
scanf("%d", &q);
int now = ;
while (q--) {
int op;
scanf("%d", &op);
if (op == ) {
scanf("%s", str + );
len[++now] = strlen(str + );
toInset(&T[], str, , len[now] / , , , now);
toInset(&T[], str, , len[now] / , -, , now);
toInset(&T[], str, len[now] / + , len[now], , , now);
toInset(&T[], str, len[now] / + , len[now], -, , now);
} else {
scanf("%s%s%s%s", a + , b + , c + , d + );
++DFN, flag = false;
int lena = strlen(a + ), lenb = strlen(b + ), lenc = strlen(c + ), lend = strlen(d + );
ask(T[], a, , lena, , );
if (flag) {
printf("0\n");
continue;
}
ask(T[], b, , lenb, -, );
if (flag) {
printf("0\n");
continue;
}
ask(T[], c, , lenc, , );
if (flag) {
printf("0\n");
continue;
}
ask(T[], d, , lend, -, );
if (flag) {
printf("0\n");
continue;
}
int ans = ;
// for (int i = 1; i <= now; ++i) {
// printf("%d %d %d %d\n", vis[0][i], vis[1][i], vis[2][i], vis[3][i]);
// }
// printf("*************\n");
for (int i = ; i <= now; ++i) {
if (vis[][i] == DFN && vis[][i] == DFN && vis[][i] == DFN && vis[][i] == DFN && lena + lenb <= len[i] / && lenc + lend <= len[i] / )
ans++;
}
printf("%d\n", ans);
}
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

Barty's Computer 字典树的更多相关文章

  1. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  2. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  3. 字典树(Trie树)实现与应用

    一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...

  4. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  5. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  6. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  7. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  8. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  9. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

随机推荐

  1. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  2. XML DTD语法详解

    XML DTD详解   XML DTD详解 前情提要与本文内容介绍 前面的两篇XML相关博文: 第一篇是介绍格式正规的XML: 格式正规的XML:语法 属性 实体 处理指令 样式单 CDATA节 第二 ...

  3. C和C++中文件读写的区别

    C中采用的主要是文件指针的办法,C++中对文件的操作主要运用了“文件流”(即非标准的输入输出)的思想 eg1": #include<stdio.h> //... FILE* fp ...

  4. C#.Net使用正则表达式抓取百度百家文章列表

    工作之余,学习了一下正则表达式,鉴于实践是检验真理的唯一标准,于是便写了一个利用正则表达式抓取百度百家文章的例子,具体过程请看下面源码: 一:获取百度百家网页内容 public List<str ...

  5. Kotlin 数据类型(数值类型)

    Kotlin 的常见数据类型: 类型 范围 byte -128~127 short 32767-32768 int -2147483648~2147483647 long 92233720368547 ...

  6. iOS开发进制转换

    1.十进制转换为二进制 /** 十进制转换为二进制 @param decimal 十进制数 @return 二进制数 */ + (NSString *)getBinaryByDecimal:(NSIn ...

  7. CF1005F Berland and the Shortest Paths

    \(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...

  8. 洛谷 P2982 [USACO10FEB]慢下来Slowing down

    题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N mov ...

  9. 去除List集合中的重复值(四种好用的方法)

    最近项目中需要对list集合中的重复值进行处理,大部分是采用两种方法,一种是用遍历list集合判断后赋给另一个list集合,一种是用赋给set集合再返回给list集合. 但是赋给set集合后,由于se ...

  10. Codeforces-B-Divisors of Two Integers(思维技巧)

    Recently you have received two positive integer numbers xx and yy. You forgot them, but you remember ...