hdu 1277 全文检索(ac自动机模板)
2015-08-11 14:22
375 查看
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
Sample Output
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace
std;
#define maxn 10
char str[60006];
struct Trie
{
int next[10000*65][maxn];
int fail[10000*65];
//失配指针
int end[10000*65];
//记录数组
int root;
//根结点指针
int L;
//总长度
int NewNode()
//获取新结点并初始化
{
for(int i=0;i<maxn;i++)
{
next[L][i]=-1;
}
end[L]=-1;
return L++;
}
void Init()
//初始化
{
L=0;
root=NewNode();
}
void Insert(char *s,int id)
{
int len=strlen(s);
int j=root;
for(int i=0;i<len;i++)
{
if(next[j][s[i]-48]==-1)
//不存在该结点
{
next[j][s[i]-48]=NewNode();
}
j=next[j][s[i]-48];
}
end[j]=id;
//记录其id
}
void Build()
{
queue<int>q;
fail[root]=root;
//根结点失配指针指向自己
//根结点的孩子入队,其失配指针指向自己
for(int i=0;i<maxn;i++)
{
if(next[root][i]==-1)
//不存在该孩子
{
next[root][i]=root;
//指向自己
}
else
{
fail[next[root][i]]=root;
//失配指针指向自己
q.push(next[root][i]);
//孩子入队
}
}
int j;
while(!q.empty())
{
j=q.front();
q.pop();
for(int i=0;i<maxn;i++)
{
if(next[j][i]==-1)
//不存在该孩子,指向其父结点失配指针所指向的结点(该结点也有孩子i)
{
next[j][i]=next[fail[j]][i];
}
else
{
fail[next[j][i]]=next[fail[j]][i];
q.push(next[j][i]);
}
}
}
}
bool used[10001];
void query(char *buf,int n)
{
int len=strlen(buf);
int j=root;
int temp;
memset(used,
0, sizeof(used));
bool flag=false;
for(int i=0;i<len;i++)
{
j=next[j][buf[i]-48];
temp=j;
while(temp!=root)
{
if(end[temp]!=-1)
//该单词或字符在Trie中出现了
{
used[end[temp]]=1;
if(!flag)
{
printf("Found key:" );
flag=true;
}
printf(" [Key No. %d]",
end[temp] );
}
temp=fail[temp];
//继续找后缀串
}
}
if(!flag)
puts(
"No key can be found !" );
else
printf("\n");
}
};
Trie ac;
char temp[100];
int main()
{
int n,m,len,id;
while (scanf("%d%d",&m,&n)!=EOF)
{
getchar();
ac.Init();
memset(str,
0, sizeof(str));
for (int i=1;i<=m; i++)
{
gets(temp);
strcat(str,
temp);
}
getchar();
for (int i=1; i<=n; i++)
{
scanf("[Key No. %d] ", &id );
gets(temp);
ac.Insert(temp, id);
}
ac.Build();
ac.query(str, n);
}
return 0;
}
我们大家经常用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]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace
std;
#define maxn 10
char str[60006];
struct Trie
{
int next[10000*65][maxn];
int fail[10000*65];
//失配指针
int end[10000*65];
//记录数组
int root;
//根结点指针
int L;
//总长度
int NewNode()
//获取新结点并初始化
{
for(int i=0;i<maxn;i++)
{
next[L][i]=-1;
}
end[L]=-1;
return L++;
}
void Init()
//初始化
{
L=0;
root=NewNode();
}
void Insert(char *s,int id)
{
int len=strlen(s);
int j=root;
for(int i=0;i<len;i++)
{
if(next[j][s[i]-48]==-1)
//不存在该结点
{
next[j][s[i]-48]=NewNode();
}
j=next[j][s[i]-48];
}
end[j]=id;
//记录其id
}
void Build()
{
queue<int>q;
fail[root]=root;
//根结点失配指针指向自己
//根结点的孩子入队,其失配指针指向自己
for(int i=0;i<maxn;i++)
{
if(next[root][i]==-1)
//不存在该孩子
{
next[root][i]=root;
//指向自己
}
else
{
fail[next[root][i]]=root;
//失配指针指向自己
q.push(next[root][i]);
//孩子入队
}
}
int j;
while(!q.empty())
{
j=q.front();
q.pop();
for(int i=0;i<maxn;i++)
{
if(next[j][i]==-1)
//不存在该孩子,指向其父结点失配指针所指向的结点(该结点也有孩子i)
{
next[j][i]=next[fail[j]][i];
}
else
{
fail[next[j][i]]=next[fail[j]][i];
q.push(next[j][i]);
}
}
}
}
bool used[10001];
void query(char *buf,int n)
{
int len=strlen(buf);
int j=root;
int temp;
memset(used,
0, sizeof(used));
bool flag=false;
for(int i=0;i<len;i++)
{
j=next[j][buf[i]-48];
temp=j;
while(temp!=root)
{
if(end[temp]!=-1)
//该单词或字符在Trie中出现了
{
used[end[temp]]=1;
if(!flag)
{
printf("Found key:" );
flag=true;
}
printf(" [Key No. %d]",
end[temp] );
}
temp=fail[temp];
//继续找后缀串
}
}
if(!flag)
puts(
"No key can be found !" );
else
printf("\n");
}
};
Trie ac;
char temp[100];
int main()
{
int n,m,len,id;
while (scanf("%d%d",&m,&n)!=EOF)
{
getchar();
ac.Init();
memset(str,
0, sizeof(str));
for (int i=1;i<=m; i++)
{
gets(temp);
strcat(str,
temp);
}
getchar();
for (int i=1; i<=n; i++)
{
scanf("[Key No. %d] ", &id );
gets(temp);
ac.Insert(temp, id);
}
ac.Build();
ac.query(str, n);
}
return 0;
}
相关文章推荐
- JGroups 手册
- C#实现将图片另存到新建的文件夹中
- JEECG datagrid 列表检索条件 添加下拉级联功能
- 线程死锁
- DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解
- 真分页!从微软官网找过来的笔记
- 当遇到骗子要你转钱时,怎么把骗子弄哭……
- Android使用SVG矢量创建很酷的动态效率!
- 基本数据类型包装类
- SQLite
- UICollectionView的使用
- 杭电 1863 畅通工程 【最小生成树&&Kruskal】
- jdk,服务端模拟客户端发送请求工具类
- Android两种 旋转Bitmap方法
- Photoshop图像处理一点通-兰晓宇-专题视频课程
- 黑马程序员——iOS开发须知
- Tomcat多域名配置
- maven与nexus安装
- HttpURLConnection与HttpClient区别及联系
- ios--c DAY_11