AcWing 3259. URL映射
原题链接
中等
作者:
把这题Ac了
,
2024-12-05 19:39:17
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 110;
struct node{
string path,name;
}Url[N];
int n,m;
string isNums(string &str){
string t;
for(int i = 0;i < str.size();i++){
char c = str[i];
if(c >= '0' && c <= '9') t += c;
else {
t.clear();
return t;
}
}
int k = 0;
while(k + 1 < t.size() && t[k] == '0')k++;
return t.substr(k);
}
vector<string> check(string &path,string &str){
vector<string> res(1);//可能没有参数 只写名字就行
int i = 1,j = 1;
for(;i < path.size() && j < str.size();){ //从1跳过/
int u = i + 1,v = j + 1;
while(u < path.size() && path[u] != '/') u++;
while(v < str.size() && str[v] != '/') v++;
string a = path.substr(i,u - i),b = str.substr(j,v - j);
if(a == "<str>"){
res.push_back(b);
i = u + 1;j = v + 1;
}else if(a == "<int>"){
string t = isNums(b);
if(t.size()){
res.push_back(t);
i = u + 1;j = v + 1;
}else{
res.clear();
return res;
}
}else if(a == "<path>"){
res.push_back(str.substr(j));
return res;
}else if(a != b){
res.clear();
return res;
}else{
i = u + 1;j = v + 1;
}
}
if(i - path.size() != j - str.size()) res.clear();
return res;
}
void work(string &str){
for(int i = 0;i < n;i++){
auto res = check(Url[i].path,str);
if(res.size() > 0){
cout << Url[i].name;
for(int i = 1;i < res.size();i++){
cout << ' ' << res[i];
}
cout << endl;
return;
}
}
puts("404");
}
int main(){
cin >> n >> m;
for(int i = 0;i < n;i++) cin >> Url[i].path >> Url[i].name;
while(m--){
string str;
cin >> str;
work(str);
}
return 0;
}