元素選擇器
記錄一下父節點,暴力往上面找
注意下標 i,j要分清楚!!!
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include<vector> using namespace std; int n, m; struct Nod { int fa = 0; int rk = 0; string s; string id; } A[105]; string ask[150]; int main() { string s; scanf("%d%d", &n, &m); getline(cin, s); A[0].fa=0; for (int i = 1; i <= n; i++) { getline(cin, s); int k = 0; int len = s.length(); A[i].s = ""; A[i].id = ""; bool f = 0; for (int j = 0; j < len; j++) { if (s[j] == '.') k++; else if (f == 0 && s[j] != ' ') { if (s[j] >= 'A' && s[j] <= 'Z') s[j] = char(s[j] - 'A' + 'a'); A[i].s.append(1,s[j]); } else if (f && s[j] != ' ') { A[i].id.append(1,s[j]); } if (j + 1 < len && s[j] == ' ' && s[j + 1] == '#') { f = 1; j++; } } A[i].rk = k; for (int j = i - 1; j > 0; j--) { if (A[j].rk < A[i].rk) { A[i].fa = j; break; } } } for(int i=0;i<m;i++){ getline(cin,s); //cout<<s<<endl; int len =s.length(); int k=0;ask[k]=""; bool f=0; for(int j=0;j<len;j++){ if(s[j]=='#'){ f=1; }else if(s[j]==' '){ k++; ask[k]=""; f=0; } else{ if(!f){ if(s[j]>='A'&&s[j]<='Z')s[j]=char(s[j]-'A'+'a'); } ask[k].append(1,s[j]); } } vector<int>v; int j=k; for(int i=1;i<=n;i++){ j=k; if(ask[k]==A[i].s||ask[k]==A[i].id){ int x=A[i].fa; //cout<<x<<' '<<i<<endl; j--; if(j<0)v.push_back(i); else while(x){ if(A[x].s==ask[j]||A[x].id==ask[j]){ j--; } x=A[x].fa; //cout<<x<<endl; if(j<0){v.push_back(i);break;} } } } cout<<v.size()<<" \n"[v.size()==0]; for(int i=0;i<v.size();i++){ cout<<v[i]<<" \n"[v.size()==i+1]; } } } /* 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 */