CCF201803-3 URL映射

仿照老师的写的,理解了好久

#include<iostream>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
bool match(string &s, string &t, bool flag)
{
	int lena = s.length(), lenb = t.length();
	int ps = 0, pt = 0;
	while (ps < lena&&pt < lenb)
	{
		if (s[ps] == t[pt]) {
			ps++; pt++;
		}
		else {
			if (s[ps] != '<') return false;
			if (s[ps+1] == 'i') {
				if (flag) cout << ' ';
				bool ok = false;
				while (pt < lenb&&isdigit(t[pt]))
				{
					if (t[pt] != '0') ok = true;
					if (flag) {
						if (ok) cout << t[pt];
					}
					pt++;
				}
				if (!ok) return false;
				ps += 5;
			}
			else if (s[ps + 1] == 's') {
				if (flag) cout << ' ';
				bool ok = false;
				while (pt < lenb&&t[pt] != '/') {
					ok = true;
					if(flag) cout << t[pt];
					pt++;
				}
				if (!ok) return false;
				ps += 5;
			}
			else if (s[ps + 1] == 'p') {
				if (flag) {
					cout << ' ';
					while (t[pt]) cout << t[pt++];
				}
				return true;
			}
		}
	}
	return pt == lenb && ps == lena;
}
int main()
{
	int n, m;
	cin >> n >> m;
	string txt[100 + 10], url, names[100 + 10];
	for (int i = 0; i < n; i++) cin >> txt[i] >> names[i];
	while (m--)
	{
		cin >> url;
		int i;
		for (i = 0; i < n; i++) {
			if (match(txt[i], url, false)) {
				cout << names[i];
				match(txt[i], url, true);
				cout << endl;
				break;
			}
		}
		if (i == n) cout << "404\n";
	}
	//system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_41776911/article/details/82378141