hdu 1277 全文检索
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1277
全文检索
Description
我们大家经常用google检索信息,但是检索信息的程序是很困难编写的;现在请你编写一个简单的全文检索程序。
问题的描述是这样的:给定一个信息流文件,信息完全有数字组成,数字个数不超过60000个,但也不少于60个;再给定一个关键字集合,其中关键字个数不超过10000个,每个关键字的信息数字不超过60个,但也不少于5个;两个不同的关键字的前4个数字是不相同的;由于流文件太长,已经把它分成多行;请你编写一个程序检索出有那些关键字在文件中出现过。
Input
第一行是两个整数M,N;M表示数字信息的行数,N表示关键字的个数;接着是M行信息数字,然后是一个空行;再接着是N行关键字;每个关键字的形式是:[Key No. 1] 84336606737854833158。
Output
输出只有一行,如果检索到有关键字出现,则依次输出,但不能重复,中间有空格,形式如:Found key: [Key No. 9] [Key No. 5];如果没找到,则输出形如:No key can be found !。
Sample Input
20 10
646371829920732613433350295911348731863560763634906583816269
637943246892596447991938395877747771811648872332524287543417
420073458038799863383943942530626367011418831418830378814827
679789991249141417051280978492595526784382732523080941390128
848936060512743730770176538411912533308591624872304820548423
057714962038959390276719431970894771269272915078424294911604
285668850536322870175463184619212279227080486085232196545993
274120348544992476883699966392847818898765000210113407285843
826588950728649155284642040381621412034311030525211673826615
398392584951483398200573382259746978916038978673319211750951
759887080899375947416778162964542298155439321112519055818097
642777682095251801728347934613082147096788006630252328830397
651057159088107635467760822355648170303701893489665828841446
069075452303785944262412169703756833446978261465128188378490
310770144518810438159567647733036073099159346768788307780542
503526691711872185060586699672220882332373316019934540754940
773329948050821544112511169610221737386427076709247489217919
035158663949436676762790541915664544880091332011868983231199
331629190771638894322709719381139120258155869538381417179544
000361739177065479939154438487026200359760114591903421347697
[Key No. 1] 934134543994403697353070375063
[Key No. 2] 261985859328131064098820791211
[Key No. 3] 306654944587896551585198958148
[Key No. 4] 338705582224622197932744664740
[Key No. 5] 619212279227080486085232196545
[Key No. 6] 333721611669515948347341113196
[Key No. 7] 558413268297940936497001402385
[Key No. 8] 212078302886403292548019629313
[Key No. 9] 877747771811648872332524287543
[Key No. 10] 488616113330539801137218227609
Sample Output
Found key: [Key No. 9] [Key No. 5]
字典树简单题,开始用kmp写t了%>_<%,换了字典树过了。。
- #include<algorithm>
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<vector>
- using std::vector;
- const int Max_N = ;
- struct Node {
- int idx;
- Node *next[];
- void set() {
- idx = ;
- for (int i = ; i < ; i++) next[i] = NULL;
- }
- };
- struct Trie {
- int l;
- char text[Max_N], buf[];
- Node stack[Max_N * ], *root, *tail;
- inline void link(char *src) {
- for (int j = ; src[j] != '\0'; j++) text[l++] = src[j];
- text[l] = '\0';
- }
- void init(){
- l = ;
- tail = &stack[];
- root = tail++;
- root->set();
- }
- inline Node *newNode() {
- Node *p = tail++;
- p->set();
- return p;
- }
- inline void insert(Node *x, char *src, int idx) {
- char *p = src;
- while (*p != '\0') {
- if (!x->next[*p - '']) x->next[*p - ''] = newNode();
- x = x->next[*p - ''];
- p++;
- }
- x->idx = idx;
- }
- inline int query(Node *x, char *src) {
- char *p = src;
- while (*p != '\0') {
- if (x->idx) return x->idx;
- if (!x || !x->next[*p - '']) return -;
- x = x->next[*p - ''];
- p++;
- }
- return -;
- }
- inline void insert(char *src, int idx) {
- insert(root, src, idx);
- }
- inline int query(char *src) {
- return query(root, src);
- }
- inline void gogo() {
- vector<int> res;
- for (int i = ; text[i] != '\0'; i++) {
- int ret = query(text + i);
- if (ret != -) res.push_back(ret);
- }
- int n = res.size();
- if (n) {
- printf("Found key: ");
- for (int i = ; i < n; i++) {
- printf("[Key No. %d]%c", res[i], i < n - ? ' ' : '\n');
- }
- } else {
- puts("No key can be found !");
- }
- }
- }solve;
- int main() {
- #ifdef LOCAL
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w+", stdout);
- #endif
- int n, m;
- char buf[];
- while (~scanf("%d %d", &n, &m)) {
- solve.init();
- while (n--) {
- scanf("%s", buf);
- solve.link(buf);
- }
- getchar(); getchar();
- for (int i = ; i <= m; i++) {
- gets(buf);
- solve.insert(strchr(buf, ']') + , i);
- }
- solve.gogo();
- }
- return ;
- }
hdu 1277 全文检索的更多相关文章
- 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 ...
- hdu 1277 AC自动机入门(指针版和数组版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...
- 【HDOJ】1277 全文检索
AC自动机,静态数组,动态分配TLE. /* 1277 */ #include <iostream> #include <cstdio> #include <cstrin ...
- HDU 1277 Nested Dolls
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...
- hdu 1277 AC自动机
全文检索 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Trie(字典树)解析及其在编程竞赛中的典型应用举例
摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
随机推荐
- update field
UPDATE dbo.HotelPolicy SET HPFactorMark=TB.MarkValue FROM (select HPF.HPFRPolicyId AS ID ,CONVERT(i ...
- tip浮动提示框
今天工作中碰到要弹出tip浮动提示框,如服务器控件的ToolTip属性. 通过GOOGLE搜到了一个很好用的tip浮动提示框:TipTip jQuery Plugin. 例子如下: <!DOCT ...
- js控制台调试
在web编程的过程中js代码出现错误,可以通过console.log()将相关信息输入到控制台进行调试. 清空控制台右击选 Clear console 菜单,或者输入 clear() 都行 控制台相关 ...
- Java构造和解析Json数据的两种方法详解一
一.介绍 JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json数据,在其官网http://www.js ...
- line-height的小技巧
CSS中的line-height属性控制着文字的行间距离.通常被设置为一个无单位的值(例如:line-height:1.4),与文字尺寸是成比例的.它是排版中的一个重要的属性.太低了文字会挤在一起,太 ...
- angular 页面加载时可以调用 函数处理
转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1599.html 我希望页面加载的时候,我能马上处理页面的数据,如请求API .... 所以这样设置 在某个 ...
- gerrit 调试smtp email
- C++读取ini文件的类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类 // Ini.h: interface for the CIni class. // // ...
- hbase常用运维命令
一,基本命令: 建表:create 'testtable','coulmn1','coulmn2' 也可以建表时加coulmn的属性如:create 'testtable',{NAME => ' ...
- windows 2003 修改远程桌面连接数详细方法
Windows Server 2003默认情况下允许远程终端连接的数量是2个用户,我们可以根据需要适当增加远程连接同时在线的用户数. 单击“开始→运行”,输入“gpedit.msc”打开组策略编辑器窗 ...