CSP CCF: 201803-3 URL映射 (C++)

参考文章

【CCF-CSP】URL映射

题目来源

URL映射

数据说明

在这里插入图片描述

注意事项

  1. 对于整数参数, 不要用 int之类的整数数据类型去存储。 直接跳过开头的 0 输出就行
  2. 因为规则和待处理的URL地址都不一定是以 '/'结尾的, 所以在将规则和待处理的URL进行比较时,特别需要注意不要吵除下标了。

思路

思路想得简洁一些, 多多注意细节(比如上边的两点,是我忽略的细节(最初只拿了60分))。

  1. 先将规则和对应的名称进行存储。 数据的存储方式简单些,就拿数组就行,不需要再去构结构体啦。
  2. 对于每个待处理的URL都需要遍历一遍规则进行比较, 如果该规则能够匹配得上,再将该规则和待处理的URL比较一次并输出相关的数据。如果匹配不上,就直接输出404。
  3. 比较函数思路其实不难,看代码能看懂哈。但我自己写的时候就是没转过弯,绕来绕去的,把自己都绕晕了。

代码

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值