元素选择器

元素选择器

题目背景

在这里插入图片描述

题目描述

在这里插入图片描述

输入输出格式以及样例

在这里插入图片描述

思路

本题最主要的是对数据的处理部分,因为只要从数据中提取出相应的标签,id以及他的等级(点数),查找起来相对容易,对于所给的查找内容,当作包含后代选择器,总是从后代选择器的最后一级(点数最多)开始,然后遍历每一条html语句,如果该语句的id或者标签与查找的内容匹配,则在该语句的所有祖先中寻找查找内容的上一级标签,查找方法是从该语句网上找,如果级数小于该语句,说明是他的祖先,然后判断该祖先的id或者标签是否与当前查找的标签(id)匹配,匹配就返回成功匹配,然后再往上一级祖先中找上一级的标签,直到查找完所有的标签都有匹配成功的,才能说明该html语句符合要求,加入到数组中。
对于样例来说:
  对于第四个查找,先查找p标签,假如查找到11行,p符合,然后往上一级祖先中查找div,找到,所以11行符合,而查找到第6行,由于该行的祖先是body和head,没有div说明,不符合,再对于第五个查找,先查找p,找到11行,p符合,在11行的祖先中查找div,找到了,然后再该div的祖先中再查找div,找到了,说明11行符合,而第9行的祖先的祖先中没有div,所以不符合。
  以上是查找的方法,数据处理的部分,由于查找的内容一开始不知道是包含几级,处理起来比较麻烦,所以使用strtok函数将字符串按照空格分开成若干个即可,然后对于每个标签和id,使用tolower函数将其转化为小写方便处理判断。

代码

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
const int maxn = 110;
struct node {
       string lable, id;//标签,id
       int temp;//级数
}test[maxn];
bool f1(int& r, int& cnt, string s) {//在自己祖先中寻找上一级标签
       for (int i = r; i >= 1; i--) {
              if (test[i].temp < cnt) {
                     cnt = test[i].temp, r = i;
                     if (s == test[i].lable || s == test[i].id) return true;
              }
       }
       return false;
}
int main() {
       int n, m;
       cin >> n >> m;
       cin.ignore();
       for (int i = 1; i <= n; i++) {//提取出每一个html语句中的标签,id以及点数
              string s;
              getline(cin, s);
              int temp1 = -1, temp2 = -1;
              test[i].temp = 0;
              for (int j = 0; j < s.length(); j++) {
                     if (s[j] == '.') test[i].temp++;
                     else if (temp1 == -1 && s[j] != '#') temp1 = j;
                     else if (s[j] == '#') temp2 = j;
              }
              if (temp2 != -1) {
                     test[i].lable = s.substr(temp1, temp2 - temp1 - 1);
                     test[i].id = s.substr(temp2);
              }
              else {
                     test[i].lable = s.substr(temp1);
                     test[i].id = "";
              }
              for (int j = 0; j < test[i].lable.size(); j++) {//转换大小写统一为小写
                     test[i].lable[j] = tolower(test[i].lable[j]);
              }
       }
       for (int i = 0; i < m; i++) {//查找
              string c;
              getline(cin, c);
              //cout << "sb" << endl;
              vector<string> q;       
              vector<int> p;
              char* pch;
              char str[128];
              strcpy(str, c.c_str());
              pch = strtok(str, " ");
              while (pch != NULL) {
                     if (pch[0] != '#') 
                            for (int j = 0; j < strlen(pch); j++) 
                                   pch[j] = tolower(pch[j]); 
                     q.push_back(pch);
                     pch = strtok(NULL, " ");
              }
              //cout << "hhh" << endl;
              int sz = q.size();
              //cout << sz << endl;
              for (int j = 1; j <= n; j++) {
                     //cout << test[j].id << " " << test[i].lable << endl;
                     if (q[sz - 1] == test[j].id || q[sz - 1] == test[j].lable) {
                            int k = sz - 2;
                            bool flag = true;
                            int temp = test[j].temp; 
                            int ins = j; 
                            for (; k >= 0; k--)
                                   if (!f1(ins, temp, q[k])) {
                                          flag = false;
                                          break;
                                   }
                            //cout << "ss" << endl;
                            if (flag)
                                   p.push_back(j);
                     }
              }
              //输出
              cout << p.size();
              for (int j = 0; j < p.size(); j++) {
                    cout << " " << p[j];
              }
              cout << endl;
       }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值