Barty's Computer 字典树
https://nanti.jisuanke.com/t/17122
Barty have a computer, it can do these two things.
Add a new string to its memory, the length of this string is even.
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
题目来源
首先对于每一个主串,都把它对半砍,前缀加入字典树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 字典树的更多相关文章
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- 字典树(Trie树)实现与应用
一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
随机推荐
- 多线程学习-基础( 十一)synchronized关键字修饰方法的简单案例
一.本案例设计到的知识点 (1)Object的notify(),notifyAll(),wait()等方法 (2)Thread的sleep(),interrupt(). (3)如何终止线程. (4)如 ...
- 手动通知扫描SD卡主动生成缩略图
最近做项目遇到的难题,调用系统拍照获取不到缩略图,非得关机重启才会生成,所以我们要主动通知系统扫描SD卡生成缩略图, 在Android4.4之前也就是以发送一个Action为“Intent.ACTIO ...
- 20169219《Linux内核原理与分析》课程总结
第一周作业 这周主要学习l了linux基础入门这个课,还是很有用的.一周学完,前两个可能做起来比较慢,但是后面就快了,不要放弃希望,你可以的! 我就是没写到博客里面,哼哼,你就不要看了.自己总结吧,摊 ...
- html5 canvas绘制矩形和圆形
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 洛谷 P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- CHImage
1.启动图.图标 iOS应用图片尺寸制作脚本 ======= github 规格尺寸(官方) AppIcon应用图标 and Launchimage启动图标的制作 iPhone所有手机型号屏幕尺寸 2 ...
- Python之‘’控制流‘’
一.if语句 格式: i1 = 3 if i1 > 4: print('yes you are right') elif 0 < i1 < 4: print('im dont kon ...
- (Python OpenGL)【4】Uniform变量 PyOpenGL
(Python OpenGL) 原文:http://ogldev.atspace.co.uk/www/tutorial05/tutorial05.html(英文) __author__ = " ...
- 最短路+状压DP【洛谷P3489】 [POI2009]WIE-Hexer
P3489 [POI2009]WIE-Hexer 大陆上有n个村庄,m条双向道路,p种怪物,k个铁匠,每个铁匠会居住在一个村庄里,你到了那个村庄后可以让他给你打造剑,每个铁匠打造的剑都可以对付一些特定 ...
- linux中断和异常睡眠问题
中断和异常: 中断只代表异步中断,异常代表同步中断,这样系统调用是异常处理,不是中断处理. 这里异常处理是可以休眠block的,因为异常处理所需的数据是存储在异常栈中,而每个进程都有一个异常栈,所以异 ...