// 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;
}
第十三次 ccf URL映射 [字符串匹配 90分]
最新推荐文章于 2020-09-07 20:58:31 发布