url映射

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

 

posted @ 2019-03-05 16:23  萌新上路  阅读(462)  评论(0编辑  收藏  举报