字典树全WA求调
查看原帖
字典树全WA求调
205359
_zzoce楼主2023/11/16 15:17
#include <iostream>
using namespace std;
#define N 1010//N棵树
#define L 5010//每棵树字符长度
void insert (string str,int trie[][30],int *tot,bool end[]){
    int len=str.size(),p=1,ch;
    for (int i=0;i<len;i++){
        ch=str[i]-'a';
        if (!trie[p][ch]){
            trie[p][ch]=++*tot;
        }
        p=trie[p][ch];
    }
    end[p]=true;
}
bool search(string str,int trie[][30],bool end[]){
    int len=str.size(),p=1;
    for (int i=0;i<len;i++){
        p=trie[p][str[i]-'a'];
        if (!p){
            return false;
        }
    }
    return end[p];
}
int trie[N][L][30],tot[N];
bool end1[N][L];
int main ()
{
    string str;
    int n,m,t;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>t;
        while(t--){
            cin>>str;
            insert(str,trie[i],&tot[i],end1[i]);
        }
    }
    
    cin>>m;
    while (m--){
        cin>>str;
        
        bool flag=false;
        for (int i=1;i<=n;i++){
            if (search(str,trie[i],end1[i])){
                if (flag){
                    cout<<" ";
                }
                cout<<i;
                flag=true;
            }
        }
        
        cout<<endl;
    }
    return 0;
}
2023/11/16 15:17
加载中...