全部评论 1

  • #include<bits/stdc++.h>
    using namespace std;
    const int N=3000005;
    int T,q,n,t[N][65],cnt[N],idx;
    char str[N];
    int getnum(char x){
        if(x>='A'&&x<='Z') return x-'A';
        else if(x>='a'&&x<='z') return x-'a'+26;
        else return x-'0'+52;
    }
    void insert(){
    	int root=0;
        int len=strlen(str);
    	for(int i=0;i<len;i++){
    		int c=getnum(str[i]);
    		if(!t[root][c]) t[root][c]=++idx;
    		root=t[root][c];
            cnt[root]++;
    	}
    }
    int find(){
    	int root=0;
        int len=strlen(str);
    	for(int i=0;i<len;i++){
    		int c=getnum(str[i]);
    		if(!t[root][c]) return 0;
    		root=t[root][c];
        } 
    	return cnt[root];
    }
    int main(){
        cin>>T;
        while(T--){
            cin>>n>>q;
            for(int i=0;i<=idx;i++){
                for(int j=0;j<=122;j++){
                    t[i][j]=0;
                }
            }
            for(int i=0;i<=idx;i++){
                cnt[i]=0;
            }
            idx=0;
            for(int i=1;i<=n;i++){
                cin>>str;
                insert();
            }
            for(int i=1;i<=q;i++){
                cin>>str;
                cout<<find()<<endl;
            }
        }
    }
    

    2天前 来自 广东

    0
暂无数据

提交答案之后,这里将显示提交结果~

首页