全文检索

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2553    Accepted Submission(s): 853

Problem 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]
 
 
题意:给一大串文本串,在给若干关键串,问关键串是否在文本串出现过
 
题解:将关键串建树,用文本串去匹配关键串。在匹配的时候,要不断将开始匹配的地址后推,
举个例子:假设关键串是123,文本串是456123;
先用456123匹配
再用56123匹配
6123
123
 
注意:文本串是连续的一大段,只是输入的时候分开输入,用strlen()求长度会超时,注意输出格式
 
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
using namespace std;
int tree[][], vis[];
int id, root, len, n, m, num = , flag = , k = ;
string s;
char str[]; void insert(int cnt)
{
root = ;
for (int i = ; s[i]; i++)//如果用strlen()求长度会超时
{
id = s[i] - '';
if (!tree[root][id])
tree[root][id] = ++num;
root = tree[root][id];
}
vis[root] = cnt;
}
void search(char ss[])
{
root = ;
for (int i = ; i < ss[i]; i++)
{
id = ss[i] - '';
if (root&&vis[root])
{
if (flag == )
{
cout << "Found key:";
flag = ;
}
cout << " [Key No. " << vis[root] << ']';
vis[root] = ;//避免重复检索
}
if (!tree[root][id])
return;
root = tree[root][id];
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
while (n--)
{
string temp;
cin >> temp;
for (int i = ; i < temp.length(); i++)//文本合并
str[k++] = temp[i];
} for (int i = ; i <= m; i++)
{
string temp;
cin >> temp >> temp >> temp >> s;//cin遇到空格停止
insert(i);
}
for(int i=;str[i];i++)//依次变化起始匹配位置
search(str+i);
if (flag == )
cout << "No key can be found !" << endl;
else
cout<<endl;
return ;
}

hdu 1277 全文检索 (字典树应用)的更多相关文章

  1. hdu 1277 全文检索

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1277 全文检索 Description 我们大家经常用google检索信息,但是检索信息的程序是很困难 ...

  2. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  3. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  6. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  7. *hdu 5536(字典树的运用)

    Input The first line of input contains an integer T indicating the total number of test cases. The f ...

  8. three arrays HDU - 6625 (字典树)

    three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...

  9. HDU 6625 (01字典树)

    题意:给定两个长为n的数组a和b:重新排列a和b,生成数组c,c[i]=a[i] xor b[i]:输出字典序最小的c数组. 分析:将a中的数插入一颗01字典树a中:将b中的数插入一颗01字典树b中: ...

随机推荐

  1. Lognormal distribution 对数正态分布

    转载:https://blog.csdn.net/donggui8650/article/details/101556041 在概率论中,对数正态分布是一种连续概率分布,其随机变量的对数服从正态分布. ...

  2. formatTime() 时间戳,返回数据是计算距离现在的时间

    const formatTime=function(tiem) {//时间转换   const timestamp = Date.now();   return function (tiem) {   ...

  3. 针对小文件的spark wholeTextFiles()

    场景:推送过来的数据文件数量很多,并且每个只有10-30M的大小 spark读取hdfs一般都是用textfile(),但是对于这种情况,如果使用textFile默认产生的分区数将与文件数目一致,产生 ...

  4. 记一次菜鸡的低级折腾--WordPress get Webshell(后台文件编辑插马)

    挺简单的一个测试站,开始思路错了,一直去网上找WordPress的漏洞,看有没有什么能利用的,未果,因为这个测试站有些地方并不完善,有的漏洞利用不了,菜鸡的我连弱口令都没猜对,没知识就是这么悲哀. 下 ...

  5. LINQ---查询表达式的结构

    重要事项: 子句必须按照一定的顺序出现 from子句和select...group子句这两部分是必须的 其他子句是可选的 在LINQ查询表达式中,select子句在表达式最后. 可以后任意多的from ...

  6. P1077 互评成绩计算

    P1077 互评成绩计算 转跳点:

  7. hello程序的运行过程-从计算机系统角度

    hello程序的运行过程-从计算机系统角度 1.gcc编译器驱动程序读取源程序文件hello.c,并将它翻译成一个可执行目标文件hello.翻译过程分为四个阶段:预处理阶段,编译阶段,汇编阶段,链接阶 ...

  8. Koa原理和封装

    相关文章 最基础 实现一个简单的koa2框架 实现一个简版koa koa实践及其手撸 Koa源码只有4个js文件 application.js:简单封装http.createServer()并整合co ...

  9. gem5-gpu 运行 PARSEC2.1

    PARSEC是针对共享内存多核处理器(CPU)的一套基准测试程序,详细介绍见wiki:http://wiki.cs.princeton.edu/index.php/PARSEC,主要参考:http:/ ...

  10. 打开exe并传参

    shellexecute(Application.Handle,'open',PWideChar('E:\控件\TMS.Scripter.Studio.Pro..6.0.2.0.Delphi.BCB. ...