1.trie tree
基本操作:
1)插入
void insert(){
len=strlen(s);
root=0;
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!trie[root][id])
trie[root][id]=++tot;
sum[trie[root][id]]++;
root=trie[root][id];
}
}
2)查询
int search(){
root=0;
len=strlen(s);
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!trie[root][id]) return 0;
root=trie[root][id];
}
return sum[root];
}
例题:Problem - 1251
const int MAX=1000010;
int trie[MAX][26];
int len,root,tot,sum[MAX];
bool p;
int n,m;
char s[11];
void insert(){
len=strlen(s);
root=0;
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!trie[root][id])
trie[root][id]=++tot;
sum[trie[root][id]]++;
root=trie[root][id];
}
}
int search(){
root=0;
len=strlen(s);
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!trie[root][id]) return 0;
root=trie[root][id];
}
return sum[root];
}
int main(){
while(gets(s)){
if(strlen(s)==0) break;
insert();
}
while(cin>>s){
cout<<search()<<endl;
}
return 0;
}
2.KMP
精髓 getfail:
思想:用自己匹配自己
void getfail(char *P,int *f){
int m=strlen(P);
f[0]=0;
f[1]=0;
for(int i=1;i<m;i++){
int j=f[i];
while (j&&P[i]!=P[j]) {
j=f[j];
}
f[i+1]=P[i]==P[j]?j+1:0;
}
}
void find(char *T,char *P,int *f){
int n=strlen(T);
int m=strlen(P);
int j=0;
getfail(P,f);
for(int i=0;i<n;i++){
while (j&&P[j]!=T[i]) {
j=f[j];
}
if(T[i]==P[j]) j++;
if(j==m) cout<<i-m+1<<endl;//找到
}
}
3.AC自动机
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 98264 Accepted Submission(s): 33942
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
Author
Wiskey
Recommend
lcy
代码
const int MAX=100010;
int cnt;
int cntword[MAX];//单词出现的次数
int fail[MAX];//失配匹配
int trie[MAX][26];
void insert(string s){//同trie
int root=0;
int n=s.size();
for(int i=0;i<n;i++){
int next=s[i]-'a';
if(!trie[root][next]){
trie[root][next]=++cnt;
}
root=trie[root][next];
}
cntword[root]++;
}
void getfail(){//queue实现
queue<int>q;
for(int i=0;i<26;i++){
if(trie[0][i]){
fail[trie[0][i]]=0;
q.push(trie[0][i]);
}
}
while (!q.empty()) {
int now=q.front();
q.pop();
for(int i=0;i<26;i++){
if(trie[now][i]){//成功匹配的话 失败-下一个指向适配匹配的下一个
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}
else{
trie[now][i]=trie[fail[now]][i];//失败的话 指向失败匹配的下一个
}
}
}
}
int query(string s){
int n=s.size();
int now=0;
int ans=0;
for(int i=0;i<n;i++){//逐个匹配
now=trie[now][s[i]-'a'];
for(int j=now;cntword[j]!=-1;j=fail[j]){
ans+=cntword[j];
cntword[j]=-1;
}
}
return ans;
}
int main(){
int n;
string s;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s;
insert(s);
}
fail[0]=0;
getfail();
cin>>s;
cout<<query(s)<<endl;
}