csp201809-03元素选择器

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<cctype>
#include<deque>
using namespace std;

int point(string s) {
	int ans = 0;
	for (auto&ch : s) {
		if (ch == '.') ++ans;
		else break;
	}
	return ans;
}

void all_tolow(string& s) {for (auto&ch : s)  ch = tolower(ch);}

bool matchsign(string s,string q) {
		stringstream ss(s);
		string t;
		getline(ss, t, ' ');
		all_tolow(t);
		all_tolow(q);
		return t == q;
}

bool matchid(string s, string q) {
	int f =s.find('#');
	if (f != -1) {
		return s.substr(f) == q;
	}
	return false;
}

bool match(string s, string q) {return q.front()=='#'? matchid(s, q): matchsign(s, q);}

struct ac {
	string c;
	int level;
	ac(string a, int b) {
		c = a;
		level = b;
	}
};

int main() {
	int n, m;
	cin >> n >> m;
	cin.get();
	vector<vector<string>> content(n);
	deque<ac> temp;
	int last = -1;
	for (int i = 0; i < n; ++i) {
		string s;
		getline(cin, s);
		int now = point(s) / 2;
		while (now <= last) {
			if (temp.size()) {
				temp.pop_back();
				last = temp.back().level;
			}
			
		}
		temp.emplace_back(ac(s.substr(now * 2), now));
		last = now;
		for (int j = 0; j < temp.size(); ++j) content[i].emplace_back(temp[j].c);
	}
	for (int i = 0; i < m; ++i) {
		string s;
		getline(cin, s);
		string temp;
		vector<string> q;
		vector<int> ans;
		stringstream ss(s);
		while (getline(ss, temp,' ')) {
			if (temp == "") continue;
			q.emplace_back(temp);
		}
		if (q.size() == 1) {
			for (int j = 0; j < n; ++j)
				if (match(content[j].back(), q[0])) ans.emplace_back(j + 1);
		}
		else {
			for (int j = 0; j < n; ++j) {
				if (match(content[j].back(), q.back())) {
					int count = 0, k = 0;
					while (count < q.size() - 1 && k < content[j].size() - 1) {
						if (match(content[j][k], q[count])) ++count;
						++k;
					}
					if (count == q.size() - 1) ans.emplace_back(j + 1);
				}
			}
		}
		cout << ans.size() << " ";
		for (int j = 0; j < ans.size(); ++j) cout << ans[j] << " ";
		cout << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值