#include<iostream>
#include<algorithm>
#include<ctype.h>
#include<string>
#include<string.h>
#include<fstream>
#include<vector>
#include<queue>
#include"iomanip"
using namespace std;
string p[105],r[105];
bool test(string url) //URL位址合法性檢測
{
if(url[0] != '/') return false;
for(int i=0;i<url.length();i++)
{
if( (url[i]>='a' && url[i]<='z') || (url[i]>='A' && url[i]<='Z') || (url[i]>='0' && url[i]<='9') || url[i]=='/' || url[i]=='-' || url[i]=='_' || url[i]=='.')
continue;
else
return false;
}
return true;
}
bool match_str(string url) //str參數比對
{
for(int i=0;i<url.length();i++)
{
if( (url[i]>='a' && url[i]<='z') || (url[i]>='A' && url[i]<='Z') || (url[i]>='0' && url[i]<='9') || url[i]=='-' || url[i]=='_' || url[i]=='.')
continue;
else
return false;
}
return true;
}
bool match_int(string url)
{
for(int i=0;i<url.length();i++)
{
if( (url[i]>='0' && url[i]<='9') )
continue;
else
return false;
}
return true;
}
bool match_path(string url)
{
for(int i=0;i<url.length();i++)
{
if( (url[i]>='a' && url[i]<='z') || (url[i]>='A' && url[i]<='Z') || (url[i]>='0' && url[i]<='9') || url[i]=='/' || url[i]=='-' || url[i]=='_' || url[i]=='.')
continue;
else
return false;
}
return true;
}
bool match(string url,string p,string r) //url位址與p規則比對檢測
{
//cout<<endl;
string s1[20],s2[20];
int x=0,y=0;
int first = -1,second = -1;
for(int i=0;i<p.length();i++) //拆分規則p
{
if(p[i] == '/' && first == -1 ) first = i;
else if(p[i] == '/' && first != -1 && second == -1)
{
second = i;
s1[x] = p.substr(first+1,second-first-1);
//cout<<s1[x]<<endl;
x++;
first = i;
second = -1;
}
else if(i == p.length()-1 && p[i]!='/')
{
second = i+1;
s1[x] = p.substr(first+1,second-first-1);
//cout<<s1[x]<<endl;
x++;
}
}
//cout<<endl;
first = -1,second = -1;
for(int i=0;i<url.length();i++) //拆分url
{
if(url[i] == '/' && first == -1 ) first = i;
else if(url[i] == '/' && first != -1 && second == -1)
{
second = i;
s2[y] = url.substr(first+1,second-first-1);
//cout<<s2[y]<<endl;
y++;
first = i;
second = -1;
}
else if(i == url.length()-1 && url[i]!='/')
{
second = i+1;
s2[y] = url.substr(first+1,second-first-1);
//cout<<s2[y]<<endl;
y++;
}
}
int k=0,i,j;
r += " ";
for(i=0;i<x;i++)
{
if(s1[i] == "<str>")
{
if(match_str(s2[k]) == true)
{
r += s2[k] + " ";
k++;
}
else
break;
}
else if(s1[i] == "<int>")
{
if(match_int(s2[k]) == true)
{
for(j=0;j<s2[k].length();j++)
{
if(s2[k][j] != '0')
break;
}
r += s2[k].substr(j) + " ";
k++;
}
else break;
}
else if(s1[i] == "<path>")
{
for(j=k;j<y;j++)
{
if(j != y-1) r += s2[j] +"/";
else r += s2[j];
}
cout<<r<<endl;
return true;
}
else
{
if(s1[i] == s2[k])
{
k++;
}
else return false;
}
if(k == y) break;
}
if(k == y && i == x-1)
{
if((url[url.length()-1] == '/' && p[p.length()-1] != '/') || (url[url.length()-1] != '/' && p[p.length()-1] == '/'))
{
//坑點一 : 占了40分,結尾的 / 不是分割符,是屬于路徑名 (題目未保證最後以/結尾)。 因為是采取以分割符進行分塊處理的做法,是以在結尾要判斷分割符。
return false;
}
cout<<r<<endl;
return true;
}
return false;
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
cin>>p[i]>>r[i];
}
for(int i=0;i<m;i++)
{
string url;
cin>>url;
if(test(url) == false)
{
cout<<"404"<<endl;
continue;
}
int j;
for(j=0;j<n;j++)
{
if(match(url,p[j],r[j]) == true)
{
break;
}
}
if(j == n) cout<<"404"<<endl;
}
return 0;
}
轉載于:https://www.cnblogs.com/fzuhyj/p/10477597.html