P3879 [TJOI2010] 阅读理解

P3879 [TJOI2010]阅读理解

STL
Trie+bitset
想练习tire的我被卡的一愣愣的qaq。数组开的小了是WA#11.开成1000就是前面十个点随机MLE。?

具体怎么用tire过这个题就是把标记出现的bool数组改成bitset,节省32倍空间,你,值得拥有。?

定义一个bitset后当二维数组用就好啦。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+6;
char s[25];
struct node
{
    
    
    int son[26];
    bool mark;
} trie[maxn];
int root=0,num=0;
bitset<1005>bt[maxn];
int n,m;
bool insert_check(char *s,int id)
{
    
    
    int pos=root;
    for(int i=0; s[i]; i++)
    {
    
    
        int now=s[i]-'a';
        if(!trie[pos].son[now])
            trie[pos].son[now]=++num;
        pos=trie[pos].son[now];
    }
    bt[pos][id]=1;
    int tmp=trie[pos].mark;
    trie[pos].mark=1;
    return tmp!=0;
}

void find_check(char *s)
{
    
    
    int pos=root;
    for(int i=0; s[i]; i++)
    {
    
    
        int now=s[i]-'a';
        if(!trie[pos].son[now])
        {
    
    
            printf("\n");
            return ;
        }
        pos=trie[pos].son[now];
    }
    for(int i=1; i<=n;i++)
    {
    
    
        if(bt[pos][i])
            printf("%d ",i);
    }
    printf("\n");
}
int main()
{
    
    

    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
    
    
        scanf("%d",&m);
        for(int j=1; j<=m; j++)
        {
    
    
            scanf("%s",s);
            insert_check(s,i);
        }
    }
    scanf("%d",&m);
    for(int i=1; i<=m; i++)
    {
    
    
        scanf("%s",s);
        find_check(s);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43653111/article/details/102596022