Problem Description
We often use google to retrieve information, but the program is very difficult to retrieve information written; now you write a simple full-text search program.
Description of the problem is this: given a file stream, digital information is completely composed of digits no more than 60,000, but less than 60; to give a fixed set of keywords, wherein the number of keywords is not more than 10,000, the digital information for each keyword not more than 60, but less than five; the first four digits of two different keywords are not identical; stream file is too long since, it has been put into multi-line; you write a program that retrieves those keywords appeared in the file.
Input
The first two rows are integers M, N; M represents the number of rows of digital information, N is the number of keywords; followed by M rows of digital information, and a blank line; then followed by N row key; each form keyword is: [Key No. 1] 84336606737854833158.
Output
Only the output line, if there is retrieved keyword is present, then sequentially outputs, but can not be repeated, with a space, such as in the form: Found key: [Key No. 9] [Key No. 5]; if not found, then the output type such as: 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]
analysis:
This question can be considered the bare title
there is useless to set a weight actually can a.
This title is a look at multi-pattern matching problem is solved with ac automata
later discovered that other people only dictionary tree + enumeration matches the beginning of string over (code to achieve a little simpler than ac automata).
Two kinds of codes (pointers are implemented trie):
code (ac automata):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int inn=0x80808080;
using namespace std;
struct Node{
int id;
Node* next[10];
Node* fail;
Node(){
id=0;
for(int i=0;i<10;i++){
next[i]=NULL;
}
fail=NULL;
}
};
Node* root;
int n,m;
vector<int>ans;
queue<Node*>q;
void insertt(string a,int id){
Node* now=root;
int len=a.size();
for(int i=0;i<len;i++){
int v=a[i]-'0';
if(now->next[v]==NULL){
now->next[v]=new Node();
}
now=now->next[v];
}
now->id=id;
}
void build(){
q.push(root);
while(!q.empty()){
Node* p=NULL;
Node* temp=q.front();
q.pop();
for(int i=0;i<10;i++){
if(temp->next[i]!=NULL){
if(temp==root){
temp->next[i]->fail=root;
}else{
p=temp->fail;
while(p!=NULL){
if(p->next[i]!=NULL){
temp->next[i]->fail=p->next[i];
break;
}
p=p->next[i];
}
if(p==NULL){
temp->next[i]->fail=root;
}
}
q.push(temp->next[i]);
}
}
}
}
void ac(string a){
Node *p=root;
int len=a.size();
for(int i=0;i<len;i++){
int v=a[i]-'0';
while(p->next[v]==NULL&&p!=root){
p=p->fail;
}
p=p->next[v];
if(p==NULL){
p=root;
}
Node* temp=p;
while(temp!=root&&temp->id!=0){
ans.push_back(temp->id);
temp=temp->fail;
}
}
}
void del(Node* now){//释放(可选)
for(int i=0;i<10;i++){
if(now->next[i]!=NULL){
del(now->next[i]);
}
}
free(now);
}
int main(){
root=new Node();
cin>>n>>m;
string all;
for(int i=1;i<=n;i++){
string t;
cin>>t;
all+=t;
}
for(int i=1;i<=m;i++){
string t;
cin>>t>>t>>t;
cin>>t;
insertt(t,i);
}
build();
ac(all);
if(ans.size()!=0){
cout<<"Found key:";
for(int i=0;i<(int)ans.size();i++){
cout<<' '<<"[Key No. "<<ans[i]<<']';
}
cout<<endl;
}else{
cout<<"No key can be found !"<<endl;
}
del(root);
return 0;
}
code (trie + enum):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<algorithm>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int inn=0x80808080;
using namespace std;
struct Node{
int id;
Node* next[10];
Node(){
id=0;
for(int i=0;i<10;i++){
next[i]=NULL;
}
}
};
Node* root;
vector<int>ans;
int n,m;
void insertt(string a,int id){
Node* now=root;
int len=a.size();
for(int i=0;i<len;i++){
int v=a[i]-'0';
if(now->next[v]==NULL){
now->next[v]=new Node();
}
now=now->next[v];
}
now->id=id;
}
void ffind(string a){
int len=a.size();
Node* now=root;
for(int i=0;i<len;i++){
int v=a[i]-'0';
if(now->id!=0){
ans.push_back(now->id);
return ;
}
if(now->next[v]==NULL){
return ;
}
now=now->next[v];
}
}
void del(Node* now){//释放(可选)
for(int i=0;i<10;i++){
if(now->next[i]!=NULL){
del(now->next[i]);
}
}
free(now);
}
int main(){
root=new Node();
string all;
cin>>n>>m;
for(int i=1;i<=n;i++){
string t;
cin>>t;
all+=t;
}
for(int i=1;i<=m;i++){
string t;
cin>>t>>t>>t;
cin>>t;
insertt(t,i);
}
for(int i=0;i<(int)all.size();i++){
ffind(all.substr(i,all.size()-i));
}
if(ans.size()!=0){
cout<<"Found key:";
for(int i=0;i<(int)ans.size();i++){
cout<<' '<<"[Key No. "<<ans[i]<<']';
}
cout<<endl;
}else{
cout<<"No key can be found !"<<endl;
}
del(root);
return 0;
}