csp2018-09-3_元素选择器
题目描述
题目思路
这是一个大的模拟题,其中也包含了一些对于字符串的处理。首先我们定义了一个node类型结构体表示输入的每一层的标签,其中包含label标签,id和输入的时候标签前面的点的数目cnt。由于标签label对大小写不敏感,所以为了处理方便我将所有的label标签使用tolower函数转化为小写字符。之后query数组使用存储查询记录的,ans数组存储结果。对于标签选择器和id选择器处理很简单,关键是后代选择器中需要寻找查询到父亲,这里我们只需要找到比当前cnt小的第一个标签的cnt即可,这里是使用search函数实现查找的。
代码实现
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 110;
int n,m;
string s;
vector<string> query; //存储查询语句
vector<int> ans;
struct node{
string lable,id; //标签和id
int cnt; //点的数量
}nd[N];
void ToLower(string &s){
for(int i = 0; i < s.length(); i++){
s[i]