题解 | #字符串匹配#

字符串匹配

https://www.nowcoder.com/practice/fbdc522ef958455687654b38a4ca01e0

#include<bits/stdc++.h>
using namespace std;
void toLower(string &s) {
	for (int i = 0; i < s.size(); i++) {
		s[i] = tolower(s[i]);
	}
	return;
}
bool match(string s, string m) {
	toLower(s);
	toLower(m);
	int front = 0, back = 0;
	int ps = -1, pm = -1;
	int len = s.size();
	while (ps < len) {
		front = m.find('[', pm+1);
		back = m.find(']', pm+2);
		string s1, s2;
		if (front != string::npos) {
			s1 = m.substr(pm+1, front - pm -1);
			s2 = s.substr(ps+1, s1.size());
			if (s1 != s2) {
				break;
			}
			ps += s1.size();
			pm += s1.size();
			s1 = m.substr(front + 1, back - front - 1);
			if (s1.find(s[ps+1]) == string::npos) {
				break;
			}
			ps++;
			pm = back;
		} else {
			s1 = m.substr(pm+1);
			s2 = s.substr(ps+1, s1.size());
			if (s1 != s2) {
				break;
			}
			ps = s.size();
		}
	}
	if (ps < len) {
		return false;
	} else {
		return true;
	}
}
int main() {
	int n;
	string input, m;
	vector<string> words;
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			cin >> input;
			words.push_back(input);
		}
		cin >> m;
		for (int i = 0; i < n; i++) {
			if (match(words[i], m)) {
				printf("%d %s\n", i + 1, words[i].c_str());
			}
		}
	}
}

全部评论

相关推荐

02-15 22:29
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务