前言
海岛老师写的真的好,感觉代码全篇没有一句废话,自己的代码能力还是太弱了,第一回交了只拿了30分,后来看了海岛老师的博客,更换了思路交了80分,最后完善后(自己的想法还是有漏洞),最终AC,强烈推荐看海岛老师的博客题解,在此发篇博客向海岛老师致敬,https://blog.csdn.net/tigerisland45/article/details/81697594
收获
感觉此次最大的收获竟然是——isdigit(cha c),C 库函数 bool isdigit(int c) 检查所传的字符是否是十进制数字字符。
Code
#include <bits/stdc++.h>
#define MAX 105
using namespace std;
string a[MAX], b[MAX];
int n, m;
bool match(string& p, string& t, bool flag)
{
int ps = 0, ts = 0;
while (ps < p.length() && ts < t.length()) {
if (p[ps] == t[ts]) {
ps++; ts++;
} else {
if (p[ps++] != '<') return false;
else {
if (flag) cout << " ";
if (p[ps] == 'i') {
bool ok = false;
while (t[ts] != '/' && isdigit(t[ts])) {
if (t[ts] != '0')
ok = true;
if (flag && ok)
cout << t[ts];
ts++;
}
if (!ok)
return false;
ps += 4;
} else if (p[ps] == 's') {
bool ok = false;
while (t[ts] && t[ts] != '/') {
ok = true;
if (flag)
cout << t[ts];
ts++;
}
if (!ok)
return false;
ps += 4;
} else if (p[ps] == 'p') {
if (flag)
while (t[ts])
cout << t[ts++];
return true;
}
}
}
}
return ps == p.length() && ts == t.length();
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < m; i++) {
string s;
cin >> s;
bool flag = false;
for (int j = 0; j < n; j++) {
if (match(a[j], s, false)) {
flag = true;
cout << b[j] << " ";
match(a[j], s, true);
break;
}
}
if (!flag) cout << "404";
cout << endl;
}
return 0;
}