• P3879 [TJOI2010]阅读理解


    P3879 [TJOI2010]阅读理解
    做法1:map+vector(直接看代码,不解释)
    做法2:trie树,在每个单词的末尾标记上是属于哪个文章的就可以了

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <map>
    #include <cstring>
    #define inf 2147483647
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(int i=a;i<=b;++i)
    //by war
    //2019.8.12
    using namespace std;
    int n,m,q,t;
    string s;
    
    map<string,vector<int> >a;
    
    void in(int &x){
        int y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(int x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    signed main(){
        in(n);
        For(i,1,n){
            in(m);
            For(j,1,m){
                cin>>s;
                a[s].push_back(i);
            }
        }
        in(q);
        For(ii,1,q){
            cin>>s;
            t=a[s].size()-1;
            For(i,0,t)
                if(i==0||a[s][i]!=a[s][i-1]){
                    if(i!=0) p(' ');
                    o(a[s][i]);
                }
            p('
    ');
        }
        return 0;
    }
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<bitset>
    #include<set>
    #include<map>
    #include<stack>
    #include<cstring>
    #pragma GCC optimize(2)
    #define inf 2147483647
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson ls,nl,mid,l,r
    #define rson rs,mid+1,nr,l,r
    #define N 1000010
    #define For(i,a,b) for(int i=a;i<=b;++i)
    #define p(a) putchar(a)
    #define g() getchar()
    //by war
    //2019.8.12
    using namespace std;
    int n,m,q,cnt;
    char s[1010];
    bool flag;
    bitset<1001> b[500007];
    
    void in(int &x){
        int y=1;char c=g();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=g();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g();}
        x*=y;
    }
    void o(int x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    namespace AC{
        int tr[N][26],tot;
        int fail[N];
        void insert(char *s,int id){
            int u=0;
            for(int i=1;s[i];i++){
                if(!tr[u][s[i]-'a']) tr[u][s[i]-'a']=++tot;
                u=tr[u][s[i]-'a'];
            }
            b[u][id]=1;
        }
        void query(char *t){
            int u=0,flag=0;
            for(int i=1;t[i];i++){  
                if(t[i]<'a'||!tr[u][t[i]-'a']){
                    flag=1;
                    break;
                }               
                u=tr[u][t[i]-'a'];
            }
            if(!flag)
                For(i,1,n)
                    if(b[u][i]){
                        o(i);p(' ');
                    }
            p('
    ');
        }
    }
    
    int main(){
        in(n);
        For(i,1,n){
            in(m);
            For(j,1,m){
                scanf("%s",s+1);
                AC::insert(s,i);
            }
        }
        in(q);
        For(i,1,q){
            scanf("%s",s+1);
            AC::query(s);
        }
        return 0;
    }
  • 相关阅读:
    密文搜索
    poj 1182 食物链
    1147. Heaps (30)
    1146. Topological Order (25)
    1145. Hashing
    1142. Maximal Clique (25)
    fzu 2112 tickets
    51nod 1554 欧姆诺姆和项链
    codeforces 963B Destruction of a Tree
    hdu 3294 Girls' research
  • 原文地址:https://www.cnblogs.com/war1111/p/11340522.html
Copyright © 2020-2023  润新知