CCF 201809-3 元素选择器

试题编号: 201809-3
试题名称: 元素选择器
时间限制: 1.0s
内存限制: 256.0MB
问题描述:



 

//本代码采用非递归暴力循环匹配
//注意题目 标签大小写不敏感,id大小写敏感
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
const int N=1e5+5;
struct node{
    string name,plus;int dj;
    node(){}
    node(string a,string b,int c):name(a),plus(b),dj(c){}
}d[105];
string s,at,t;int n,m,sz,cnt,res[N],ans[N];vector<string> v;
inline void stdit(string &s){transform(s.begin(),s.end(),s.begin(),::tolower);}
void readstable(){
    getline(cin,s);at="";
    int rk=0,pos=0;
    rk=s.find_first_not_of('.');
//    s.erase(s.find_last_not_of(' ')+1);
    s=s.substr(rk);
    rk>>=1;
    pos=s.find('#');
    if(~pos){
        stringstream i(s);
        i>>s;
        i>>at;
    }
    stdit(s);
    d[++cnt]=node(s,at,++rk);
}
inline void ins(string s){if(s[0]!='#') stdit(s);v.pb(s);}
void split(){
    v.clear();
    getline(cin,s);
//    s.erase(s.find_last_not_of(' ')+1);
    int pos=s.find_first_of(' ');
    if(~pos){
        do{
            ins(s.substr(0,pos));
            s=s.substr(pos+1);
            pos=s.find_first_of(' ');
        }while(~pos);
    }
    ins(s);
}
void dfs(int cur,int cj,int lim=0){
    if(cur==sz){
        ans[++ans[0]]=res[sz-1];
        return ;
    }
    for(int j=lim+1;j<=n;j++){
        if(d[j].dj<=cj) return ;
        if(d[j].name==v[cur]||d[j].plus==v[cur]){
            res[cur]=j;
            dfs(cur+1,d[j].dj,j);
        }
    }
}
void query(){
    split();
    sz=v.size();
    int &cnt=ans[0];cnt=0;
    dfs(0,0,0);
    sort(ans+1,ans+cnt+1); 
    cnt=unique(ans+1,ans+cnt+1)-(ans+1);
    if(!cnt){puts("0");return ;}
    printf("%d ",cnt);
    for(int i=1;i<=cnt;i++) printf("%d ",ans[i]);
    puts("");
}
int main(){
    scanf("%d%d\n",&n,&m);
    for(int i=0;i<n;i++) readstable();
    for(int i=0;i<m;i++) query();
    return 0;
}
/*
11  5 
html 
..head 
....title 
..body 
....h1 
....p #subtitle 
....div #main 
......h2 
......p #one 
......div 
........p #two 
p 
#subtitle 
h3 
div p 
div div p 
*/

 

posted @ 2020-03-13 20:25  神犇(shenben)  阅读(226)  评论(0编辑  收藏  举报