http://acm.hdu.edu.cn/showproblem.php?pid=2222
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 63356 Accepted Submission(s): 21019
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1 5 she he say shr her yasherhs
Sample Output
3
分享几篇好的博客http://blog.csdn.net/niushuai666/article/details/7002823(AC自动机)这篇博客访问量60000多,讲的很清楚,刚开始接触看的时候,不追求看懂,主要是他的大概意思,细节问题还是要去推代码才能懂的。
http://blog.csdn.net/v_july_v/article/details/6897097这篇是字典树的博客,只用看字典树的部分。
学AC自动机之前要会KMP(AC自动机需要用的是KMP思想)和字典树。KMP的博客我发的有几篇模板,可以看在这之前的几篇。
到现在我就知道AC自动机有两种代码,一种是指针型的,一种是数组型的,前者相对好理解,后者一定要在前者理解后再去理解更好一点(个人认为,我就是这么学的)。
个人感觉这些代码都不是特别难理解,先看博客了解个大概,再一步一步慢慢模拟,用不了多长时间就能懂的。
还有一种暴力过的AC代码:(时间长,但是代码非常短)
Status | Accepted |
---|---|
Time | 795ms |
Memory | 53496kB |
Length | 1191 |
Lang | G++ |
Submitted | 2017-06-22 20:16:48 |
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
struct node
{
int num;
node *next[26];
}*root;
void join(const char *s)
{
node *p=root,*t;
int i,len=strlen(s);
for(i=0; i<len; i++)
if(p->next[s[i]-'a'])
p=p->next[s[i]-'a'];
else
{
t=new node;
memset(t,0,sizeof(node));
p->next[s[i]-'a']=t;
p=t;
}
p->num++;
}
void work(const char *s)
{
node *p;
int i,j,len=strlen(s),sum=0;
for(i=0; i<len; i++)
{
p=root;
for(j=i; j<len; j++)
{
if(p->next[s[j]-'a'])
{
p=p->next[s[j]-'a'];
sum+=p->num;
p->num=0;
}
else
break;
}
}
printf("%d\n",sum);
}
void init()
{
int num;
char t[60];
root=new node;
memset(root,0,sizeof(node));
scanf("%d",&num);
while(num--)
{
scanf("%s",t);
join(t);
}
}
int main()
{
int exp;
char t[1000010];
scanf("%d",&exp);
while(exp--)
{
init();
scanf("%s",t);
work(t);
}
}
指针型的AC代码:(找了很多代码,就这个代码运行时间最短,代码不算长)
Status | Accepted |
---|---|
Time | 234ms |
Memory | 53428kB |
Length | 2114 |
Lang | G++ |
Submitted | 2017-06-22 20:08:40 |
#include <iostream>
#include <queue>
#include<cstring>
#include<cstdio>
using namespace std;
struct Trie
{
int cnt;
Trie* next[26], *prefix;
} Table[500000];
int index= 0;
Trie* root;
char str[1000001];
void init()
{
index= 0;
root= &Table[index];
memset( Table[0].next, 0, sizeof(Table[0].next) );
Table[0].cnt= 0;
Table[0].prefix= NULL;
}
void insert( char* s )
{
Trie* r= root;
while( *s )
{
int t= *s- 'a';
if( !r->next[t] )
{
++index;
memset( Table[index].next, 0, sizeof(Table[index].next) );
Table[index].cnt= 0;
Table[index].prefix= NULL;
r->next[t]= &Table[index];
}
r= r->next[t];
s++;
}
r->cnt++;
}
void get_prefix()
{
queue<Trie*> q;
q.push( root );
root->prefix= NULL;
while( !q.empty() )
{
Trie* father= q.front();
q.pop();
for( int i= 0; i< 26; ++i )
if( father->next[i] )
{
Trie* tmp= father->prefix;
while( tmp && !tmp->next[i] ) tmp= tmp->prefix;
if( !tmp ) father->next[i]->prefix= root;
else father->next[i]->prefix= tmp->next[i];
q.push( father->next[i] );
}
}
}
int ac_run()
{
Trie* p= root;
int ans= 0;
char* s= str;
while( *s )
{
int t= *s- 'a';
while( !p->next[t] && p!= root ) p= p->prefix;
p= p->next[t];
if( !p ) p= root;
Trie* tp= p;
while( tp!= root && tp->cnt!= -1 )
{
ans+= tp->cnt;
tp->cnt= -1;
tp= tp->prefix;
}
s++;
}
return ans;
}
int main()
{
int test;
scanf("%d",&test );
while( test-- )
{
int n;
char s[55];
scanf("%d",&n );
init();
getchar();
for( int i= 0; i< n; ++i )
{
gets(s);
insert(s);
}
gets(str);
get_prefix();
printf("%d\n", ac_run() );
}
return 0;
}
数组型的AC代码:(个人认为数组型的代码跟简单一些,能A掉卡内存的题(指针型的一直是超内存,所以我才学会了数组型的代码0.0))
Status | Accepted |
---|---|
Time | 327ms |
Memory | 65856kB |
Length | 1667 |
Lang | G++ |
Submitted | 2017-06-27 08:50:53 |
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define N 500005
char s[N*2];
int n,ans,T,sz;
int fail[N],ch[N][30],value[N];
bool vis[N];
queue<int> q;
void init()
{
ans = sz = 0;
memset(fail,0,sizeof(fail));
memset(value,0,sizeof(value));
memset(ch,0,sizeof(ch));
memset(vis,0,sizeof(vis));
}
void insert()
{
int len = strlen(s);
int now = 0;
for( int i = 0; i < len; i++ )
{
int x = s[i] - 'a';
if( !ch[now][x] ) ch[now][x] = ++sz;
now = ch[now][x];
}
++value[now];
}
void fal()
{
while( !q.empty() ) q.pop();
for( int i = 0; i < 26; i++ )
if( ch[0][i] ) q.push(ch[0][i]);
while( !q.empty() )
{
int now = q.front();
q.pop();
for( int i = 0; i < 26; i++ )
{
if( !ch[now][i] )
{
ch[now][i] = ch[fail[now]][i];
continue;
}
fail[ch[now][i]] = ch[fail[now]][i];
q.push(ch[now][i]);
}
}
}
void ac()
{
int len = strlen(s);
int now = 0;
for( int i = 0; i < len; i++ )
{
int x = s[i] - 'a';
int y = ch[now][x];
while( y && !vis[y] )
{
vis[y] = 1;
ans += value[y];
y = fail[y];
}
now = ch[now][x];
}
}
int main()
{
scanf("%d", &T);
while( T-- )
{
init();
scanf("%d", &n);
getchar();
while( n-- )
{
gets(s);
insert();
}
gets(s);
fal();
ac();
printf("%d\n", ans);
}
return 0;
}