【HDOJ】1277 全文检索
AC自动机,静态数组,动态分配TLE。
/* 1277 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXL 60005
#define TRIEN 10 typedef struct Trie {
int n;
Trie *fail;
Trie *next[TRIEN];
} Trie; Trie tries[];
int nTrie;
char s[MAXL];
char str[];
int ans[], n;
bool flag;
Trie *root; Trie *newNode() {
memset(tries[nTrie].next, , sizeof(tries[nTrie].next));
tries[nTrie].n = ;
tries[nTrie].fail = NULL;
return &tries[nTrie++];
} void create(char str[], int v) {
int i = , id;
Trie *p = root, *q; while (str[i]) {
id = str[i] - '';
++i;
if (p->next[id] == NULL) {
q = newNode();
p->next[id] = q;
}
p = p->next[id];
}
p->n = v;
} void build_fail() {
int i;
Trie *p, *q;
queue<Trie *> Q; for (i=; i<TRIEN; ++i) {
if (root->next[i]) {
root->next[i]->fail = root;
Q.push(root->next[i]);
}
} while (!Q.empty()) {
p = Q.front();
Q.pop();
for (i=; i<TRIEN; ++i) {
if (p->next[i]) {
q = p->fail;
while (q) {
if (q->next[i]) {
p->next[i]->fail = q->next[i];
break;
}
q = q->fail;
}
if (q == NULL)
p->next[i]->fail = root;
Q.push(p->next[i]);
}
}
}
} void search(char s[]) {
int i = , id;
Trie *p = root, *q; while (s[i]) {
id = s[i] - '';
++i;
while (p->next[id]==NULL && p!=root)
p = p->fail;
p = p->next[id];
if (p == NULL)
p = root;
q = p;
while (q != root) {
if (q->n) {
flag = true;
ans[n++] = q->n;
q->n = ;
}
q = q->fail;
}
}
} void del(Trie *t) {
if (t == NULL)
return ;
for (int i=; i<TRIEN; ++i)
del(t->next[i]);
free(t);
} void init() {
n = ;
flag = false;
nTrie = ;
root = newNode();
} int main() {
int t, m;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif while (scanf("%d %d", &t, &m) != EOF) {
for (i=; i<t; ++i) {
scanf("%s%*c", str);
strcat(s, str);
}
getchar();
init();
for (i=; i<=m; ++i) {
scanf("%*s %*s %*s %s", str);
create(str, i);
}
build_fail();
search(s);
if (flag) {
printf("Found key:");
for (i=; i<n; ++i)
printf(" [Key No. %d]", ans[i]);
printf("\n");
} else {
printf("No key can be found !\n");
}
} return ;
}
【HDOJ】1277 全文检索的更多相关文章
- hdu 1277 全文检索
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1277 全文检索 Description 我们大家经常用google检索信息,但是检索信息的程序是很困难 ...
- HDU 1277全文检索(字典树)
全文检索 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1277 全文检索 (直接映射查找 || 自动机)
Problem - 1277 无聊做水题的时候发现的一道题目.这道题第一反应可以用自动机来解决.当然,条件是各种限制,从而导致可以用直接映射标记的方法来搜索.具体的做法就像RK算法一样,将字符串has ...
- hdu 1277 全文检索 (字典树应用)
全文检索 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Trie(字典树)解析及其在编程竞赛中的典型应用举例
摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...
- Solr_全文检索引擎系统
Solr介绍: Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务.Solr可以独立运行在Jetty.Tomcat等这些Servlet容器中. Solr ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- cocos2dx内存管理的一些看法
今年年初进入一家游戏公司,正式开始游戏引擎的学习,之前的ios学习,对现在的游戏引擎学习还是有很大的帮助的,虽然使用c++,但却能时刻感受到ios框架对于cocos2dx的巨大影响. 由于之前一直使用 ...
- Android-Socket传输 GPRS网络
手机使用GPRS网络与server进行Socket通信,代码下载地址:http://download.csdn.net/detail/wu20093346/7768481 用UDP协议与Socket调 ...
- 从 ReactiveCocoa 中能学到什么?不用此库也能学以致用
从知道ReactiveCocoa开始就发现对这个库有不同的声音,上次参加<T>技术沙龙时唐巧对在项目中已全面使用FRP的代码家提出为什么这种编程模型出现了这么长时间怎么像ReactiveC ...
- MVC模式下的数据展示:EasyUI的datagrid
我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...
- 9.7noip模拟试题
题目名称 日历游戏 最大公约数 密码 英文代号 calendar gcd pasuwado 输入文件名 calendar.in gcd.in pasuwado.in 输出文件名 calendar.ou ...
- 重装系统时,将MBR分区转为GPT 分区
摘要 很多同学在重装系统的时候,或多或少都遇到过这样的问题:镜像文件没有问题,软碟通刻录也没有问题,但偏偏就在选择安装系统盘盘符的时候,跳出对话框,提示:Windows无法安装到这个磁盘,选中的磁盘具 ...
- 解决CSS中float:left后需要clear:both清空的繁琐步骤(转)
之前,因为公司专门有CSS+DIV的切片设计师,所以我一直都是注重程序的设计与开发.现在,因为接了一些Web网站的项目需要制作,就在空闲时间学习起了CSS.Jquery. 现在,大部分的横排导航都 ...
- c# 网站发布
.net 网站发布简单步骤: 1.选择需要发布的网站,右击->发布 1)配置文件:可以任意新建文件配置名 2)连接: 发布方法选择系统文件:选择目标位置,任意新建一个位置即可. 3)设置:选择D ...
- Oracle存储过程及函数
1.在Oracle中,存储过程包括三部分组成:定义部分.执行部分.和异常处理部分(即例外) eg1:输入员工编号,查询员工的姓名和薪资 create or repalce procedure myp ...
- 使用top工具,找出消耗CPU 较多的进程
1.使用top工具,找出消耗CPU 较多的进程 [oracle@cuug ~]$ top top - 10:48:27 up 23:15, 4 users, load average: 1.09, ...