并不对劲的trie树
听上去像是破坏植物的暴力行为(并不)。
可以快速查询某个字符串在某个字符串集中出现了几次,而且听上去比字符串哈希靠谱。
把整个字符串集建成树,边权是字符,对于字符串结尾的节点进行特殊标记。
这样一方面合并了前缀,节省空间;另一方面查询很方便,直接按边走就行。
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxn 10010
#define maxk maxn*50
#define maxm 30
#define maxb 110010
using namespace std;
int read(){
int f=,x=;char ch=getchar();
while(isdigit(ch)== && ch!='-')ch=getchar();
if(ch=='-')f=-,ch=getchar();
while(isdigit(ch))x=x*+ch-'',ch=getchar();
return x*f;
}
void write(int x){
int ff=;char ch[];
while(x)ch[++ff]=(x%)+'',x/=;
if(ff==)putchar('');
while(ff)putchar(ch[ff--]);
putchar(' ');
}
struct AC{
int cnt,len,to[maxk][maxm],n,val[maxk],m;
bool yes[maxk];
char s[];
int getnum(char c){
return int(c-'a');
}
void build(){
int u=,cur=;
for(;u<len;u++){
if(!to[cur][getnum(s[u])])
to[cur][getnum(s[u])]=++cnt;
cur=to[cur][getnum(s[u])];
}
val[cur]++,yes[cur]=;
return ;
}
void getans(){
int u=,cur=;
for(;u<len;u++){
if(!to[cur][getnum(s[u])])
{printf("WRONG\n");return;}
cur=to[cur][getnum(s[u])];
}
if(yes[cur]==)printf("WRONG\n");
else if(val[cur]==)printf("REPEAT\n");
else {val[cur]--;printf("OK\n");}
return;
}
void prt(){
for(int i=;i<=cnt;i++){
for(int j=;j<=;j++){
write(to[i][j]);
}
cout<<endl;
}
}
void init(){
cnt=;
n=read();
for(int i=;i<=n;i++){
scanf("%s",s);
len=strlen(s);
build();
}
// prt();
}
void ask(){
m=read();
for(int i=;i<=m;i++){
scanf("%s",s);
len=strlen(s);
getans();
}
}
}solve;
int main(){
solve.init();
solve.ask();
return ;
}
并不对劲的trie
其实这是AC代码
知道了这些又有什么用呢?某些字符串问题就能在树上乱搞了。
并不对劲的trie树的更多相关文章
- 并不对劲的字符串专题(三):Trie树
据说这些并不对劲的内容是<信息学奥赛一本通提高篇>的配套练习. 并不会讲Trie树. 1.poj1056-> 模板题. 2.bzoj1212-> 设dp[i]表示T长度为i的前 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
- 通过trie树实现单词自动补全
/** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...
- #1014 Trie树
本题主要是求构造一棵Trie树,即词典树用于统计单词. C#代码如下: using System; using System.Collections.Generic; using System.Lin ...
- Trie树-字典查找
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
- Trie树的创建、插入、查询的实现
原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...
随机推荐
- [Vijos1617] 超级教主(DP + 单调队列)
传送门 设 f[i] 表示吃完 f[i] 及其以下的能量球后所剩下的能量. 所以 f[i] = max(f[i], f[j] + (sum[i] - sum[j]) - i * 100) ( 0 &l ...
- 任务查询系统(bzoj 3932)
Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...
- 【译】Nodejs最好的ORM
TypeORM github: https://github.com/typeorm/typeorm这篇译文是从TypeORM github上的使用说明上翻译过来的,已经提交PR并merge到库中了. ...
- 创建SSH keys
1.检查是否已经有SSH Key存在 windows: type "%userprofile%\.ssh\id_rsa.pub" Linux: cat ~/.ssh/id_rsa. ...
- PAT (Advanced Level) 1035. Password (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT (Advanced Level) 1032. Sharing (25)
简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...
- Spring的JDBC框架概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/jdbc-framework.html: 在使用普通的JDBC操作数据库时,就会很麻烦的写很多不必要 ...
- jq自定义裁剪,代码超级简单,易修改
1.自定义宽高效果 1.html 代码 index.html <!DOCTYPE html> <html lang="en"> <head> ...
- hybird app 用 xcode ios打包 ipa 测试包并且安装真机测试
1.创建 ios 项目 1.用 cordova 创建一个 ios 项目 npm install -g cordova cordova create hello com.mydomain.hello H ...
- [React] {svg, css module, sass} support in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr ...