字符串专题总结(Trie+KM+AC自动机)

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自动机

例题Problem - 2222

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.
 

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.
 

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值