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 ...
随机推荐
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...
- try-catch-finally 规则( 异常处理语句的语法规则 )
1) 必须在 try 之后添加 catch 或 finally 块.try 块后可同时接 catch 和 finally 块,但至少有一个块. 2) 必须遵循块顺序:若代码同时使用 catch 和 ...
- HtmlAgilityPack HTML操作类库的使用
HtmlAgilityPack是.NET下的一个开源的HTML解析类库.支持用XPath来解析HTML.命名空间:HtmlAgilityPack. 1.读取网络中html网页内容,获取网页中元素bod ...
- CodeForces 658C Bear and Forgotten Tree 3 (构造)
题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker ...
- GitHub Pages搭建博客HelloWorld版
1.原理 GitHub作为博客相关文件的托管方,你把按照jekyll规定的目录及文件上传至github库中,通过约定的库名称即可访问到经过jekyll渲染后的博客页面. 2.搭建过程 2.1.注册Gi ...
- PostgreSQL 设置主键的序列值
1. 问题的提出 PostgreSQL定义TABLE时,主键的字段类型可以设定为自增类型serial,即插入每条记录时,主键的值自动加1.但是,当插入数据的时候指定了具体的主键值,例如主键值从0到50 ...
- webapi 返回json
web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...
- .net core 部署在Linux系统上运行的环境搭建总结
安装Linux用的是腾讯云的centos7.5,需要安装有环境有mysql5.7 .netcore2.1 nginx1.14 1.首先是mysql的安装 我用的链接工具是putty,首先root登入 ...
- Docker基本使用(二) Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world runoob@runoob:~$ docker run ubunt ...
- 消息队列开发记录笔记-ActiveMQ
1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...