第十三次 ccf URL映射 [字符串匹配 90分]

// stringtraining.cpp: 定义控制台应用程序的入口点。
//
#include <bits/stdc++.h>
using namespace std;
vector<string>now;
int n, m;
bool match(string a, string b)
{
    int len = b.size();
    string pre;
    pre.clear();
    int zf = 0;
    if (a == "/<int>/")
    {
        zf = 0;
        if (b[0] != '/' || b[len - 1] != '/')
        {
            return false;
        }
        for (int i = 1; i<len - 1; i++)
        {
            if (b[i]<'0' || b[i]>'9')
            {
                return false;
            }
            if (b[i] != '0') zf = 1;
            if (zf)
            {
                pre += b[i];
            }
        }
        now.push_back(pre);
        return true;
    }
    else if (a == "/<int>")
    {
        zf = 0;
        if (b[0] != '/')
        {
            return false;
        }
        for (int i = 1; i <= len - 1; i++)
        {
            if (b[i]<'0' || b[i]>'9')
            {
                return false;
            }
            if (b[i] != '0') zf = 1;
            if (zf)
            {
                pre += b[i];
            }
        }
        now.push_back(pre);
        return true;
    }
    else if (a == "/<str>/")
    {

        if (b[0] != '/' || b[len - 1] != '/')
        {
            return false;
        }
        for (int i = 1; i<len - 1; i++)
        {
            if ((b[i] >= 'a'&&b[i] <= 'z') || (b[i] >= 'A'&&b[i] <= 'Z') ||
                b[i] == '_' || b[i] == '-' || b[i] == '.' || (b[i] >= '0'&&b[i] <= '9'))
            {
                pre += b[i];
            }

            else return false;
        }
        now.push_back(pre);
        return true;
    }
    else if (a == "/<str>")
    {

        if (b[0] != '/')
        {
            return false;
        }
        for (int i = 1; i <= len - 1; i++)
        {
            if ((b[i] >= 'a'&&b[i] <= 'z') || (b[i] >= 'A'&&b[i] <= 'Z') ||
                b[i] == '_' || b[i] == '-' || b[i] == '.' || (b[i] >= '0'&&b[i] <= '9'))
            {
                pre += b[i];
            }
            else return false;
        }
        now.push_back(pre);
        return true;
    }
    else if (a == "/<path>")
    {
        if (b[0] != '/' || b[len - 1] == '/')
        {
            return false;
        }
        for (int i = 1; i <= len - 1; i++)
        {
            if ((b[i] >= 'a'&&b[i] <= 'z') || (b[i] >= 'A'&&b[i] <= 'Z') ||
                b[i] == '_' || b[i] == '-' || b[i] == '.' || (b[i] >= '0'&&b[i] <= '9') || b[i] == '/')
            {
                pre += b[i];
            }
            else return false;
        }
        now.push_back(pre);
        return true;
    }
    else
    {
        if (a.size() != b.size())
        {
            return false;
        }
        for (int i = 0; i<b.size(); i++)
        {
            if (a[i] != b[i])
            {
                return false;
            }
        }
        return true;
    }
}
bool check(string s, string k)
{
    int i = 0, j = 0;
    string a;
    string b;
    while (i<s.size() && j<k.size())
    {
        a.clear();
        b.clear();
        int cnt = 0, cnt2 = 0;
        for (; i<s.size(); i++)
        {
            if (s[i] == '/')cnt++;
            a += s[i];
            if (cnt == 2)break;
        }
        if (a == "/<path>")
        {
            for (; j < k.size(); j++)
                b += k[j];
            if (!match(a, b))return false;
        }
        else
        {
            for (; j < k.size(); j++)
            {
                if (k[j] == '/')cnt2++;
                b += k[j];
                if (cnt2 == 2)break;
            }
            if (!match(a, b))return false;
        }
    }
    if (i == s.size() && j == k.size())return true;
    else return false;

}
int main()
{
    cin >> n >> m;
    map<int, string>mp;
    vector<string>rule;
    vector<string>url;
    string t, s;
    for (int i = 1; i <= n; i++)
    {
        cin >> t >> s;
        mp[i] = s;
        rule.push_back(t);
    }
    for (int i = 1; i <= m; i++)
    {
        string k;
        cin >> k;
        url.push_back(k);
    }

    for (int i = 0; i<m; i++)
    {
        int fl = 0;
        for (int j = 0; j<n; j++)
        {
            now.clear();

            if (check(rule[j], url[i]))
            {
                cout << mp[j+1];
                for (int s = 0; s<now.size(); s++)
                {
                    cout << " " << now[s];
                }
                cout << endl;
                fl = 1;
                break;
            }
        }
        if (!fl)
        {
            cout << "404" << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值