参考文章
题目来源
数据说明
注意事项
- 对于整数参数, 不要用 int之类的整数数据类型去存储。 直接跳过开头的 0 输出就行
- 因为规则和待处理的URL地址都不一定是以 '/'结尾的, 所以在将规则和待处理的URL进行比较时,特别需要注意不要吵除下标了。
思路
思路想得简洁一些, 多多注意细节(比如上边的两点,是我忽略的细节(最初只拿了60分))。
- 先将规则和对应的名称进行存储。 数据的存储方式简单些,就拿数组就行,不需要再去构结构体啦。
- 对于每个待处理的URL都需要遍历一遍规则进行比较, 如果该规则能够匹配得上,再将该规则和待处理的URL比较一次并输出相关的数据。如果匹配不上,就直接输出404。
- 比较函数思路其实不难,看代码能看懂哈。但我自己写的时候就是没转过弯,绕来绕去的,把自己都绕晕了。
代码
#include <iostream>
#include <fstream>
using namespace std;
const int MAXN = 1e5;
string p[MAXN], r[MAXN], q;
bool match(string q, string p, bool ifcout) {
int qlen = q.size(), plen = p.size();
int qindex = 0, pindex = 0;
while (qindex < qlen && pindex < plen) {
if (q[qindex] == p[pindex]) { // 相同
++qindex;
++pindex;
}
else { // 不同
if (p[pindex] != '<') { // 不能进行匹配
return false;
}
++pindex; // +1~
// pindex < plen
if (pindex < plen && p[pindex] == 'i') { // int
// int attr = 0; // 不能 int 呀
string attr;
bool flag = true;
while (isdigit(q[qindex])) {
if ((flag == false && q[qindex] == '0') || q[qindex] != '0') { // flag == false && q[qindex] == '0'
flag = false;
attr += q[qindex];
}
++qindex;
}
if (ifcout) {
cout<<" "<<attr; //<<endl;
}
pindex += 4;
}
else if (pindex < plen && p[pindex] == 's'){
string attr;
while (qindex < qlen && q[qindex] != '/') { // !!!!!!! qindex < qlen !!!!!!!!!
attr += q[qindex];
++qindex;
}
if (ifcout) {
cout<<" "<<attr; //<<endl;
}
pindex += 4;
}
else if (pindex < plen && p[pindex] == 'p') {
if (ifcout) {
cout<<" "<<q.substr(qindex, (qlen - qindex)); //<<endl;
}
return true;
}
}
}
return (pindex == plen) && (qindex == qlen);
}
int main() {
ifstream cin("in.txt");
int n, m;
cin>>n>>m;
for (int i = 0; i <n; ++i) {
cin>>p[i]>>r[i];
}
for (int i = 0; i < m; ++i) {
cin>>q;
bool flag = false;
for (int j = 0; j < n; ++j) {
if (match(q, p[j], false)) {
flag = true;
cout<<r[j];
match(q, p[j], true);
break;
}
}
if (flag == false) {
cout<<"404";
}
cout<<endl;
}
return 0;
}