ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CCF-201803-3-URL映射

2021-03-20 19:03:02  阅读:187  来源: 互联网

标签:return URL true lenS int flag vecMp CCF 201803


题意:
给你n个URL规则,在给你m个待处理URL 地址,求解它们与哪个规则匹配,并输出相应的参数
题解:
把字符串拆分出来,逐一匹配。注意/com/和/com,/<str>/和/<str>,/<int>/和/<int>,/<path>/和/<path>是不同的
代码如下:

#include<bits/stdc++.h>  ///201803-3
using namespace std;
const int maxn=1e2+5;
int n,m;
struct node{
    int flag;
    string content;
    int pos;
};
string name[maxn],quS;
vector<node>vecMp[maxn],vecTmp;
bool flagA[maxn];
vector<string>ans;
void init(){
    int i;
    for (i=0;i<=100;i++)vecMp[i].clear();
    memset(flagA,0,sizeof(flagA));
}
bool checkNum(string s){
    int i,lenS;
    lenS=s.size();i=0;
    while(i<lenS){
        if(isdigit(s[i]));
        else return false;
        i++;
    }
    return true;
}
bool checkFu(char fu){
    if(isalpha(fu))return true;
    if(isdigit(fu))return true;
    if(fu=='-')return true;
    if(fu=='_')return true;
    if(fu=='.')return true;
    if(fu=='/')return true;
    return false;
}
bool check(string s){
    int i,lenS;
    lenS=s.size();
    for (i=0;i<lenS;i++){
        if(checkFu(s[i]));
        else return false;
    }
    return true;
}
void deal_1(int id,string s){
    node now;
    int i,lenS,lenT;
    bool flag;
    string tmpS,tmpS2;
    lenS=s.size();i=0;tmpS="";
    if(s[lenS-1]=='/')flagA[id]=true;
    while(i<lenS){
        while(i<lenS&&(s[i]=='/'))i++;
        if(i>=lenS)break;
        tmpS="";now.pos=i;
        while(i<lenS&&(s[i]!='/'))tmpS+=s[i],i++;
        if(tmpS=="")break;
        now.content=tmpS;
        lenT=tmpS.size();flag=false;
        if(tmpS[0]=='<'&&tmpS[lenT-1]=='>'){
           tmpS2="";
           tmpS2=tmpS.substr(1,lenT-2);
           if(tmpS2=="int")now.flag=2,flag=true;
           else if(tmpS2=="str")now.flag=4,flag=true;
           else if(tmpS2=="path")now.flag=3,flag=true;
           if(flag){
              vecMp[id].push_back(now);
              continue;
           }
        }
        now.flag=1;
        vecMp[id].push_back(now);
    }
}
void deal_2(string s){
     node now;
     int i,lenS;
     string tmpS;
     vecTmp.clear();
     lenS=s.size();i=0;tmpS="";
     flagA[n+1]=false;
     if(s[lenS-1]=='/')flagA[n+1]=true;
     while(i<lenS){
        while(i<lenS&&(s[i]=='/'))i++;
        now.pos=i;
        if(i>=lenS)break;
        tmpS="";
        while(i<lenS&&(s[i]!='/'))tmpS+=s[i],i++;
        if(tmpS=="")break;
        now.content=tmpS;
        now.flag=5;
        if(checkNum(tmpS))now.flag=6;
        vecTmp.push_back(now);
     }
}
bool match(int id){
    int i,j,k,len1,len2,lenS;
    string tmpS="";
    bool flag;
    if(flagA[id]!=flagA[n+1])return false;
    ans.clear();
    i=j=0;len1=vecMp[id].size();len2=vecTmp.size();
    for (;i<len1&&(j<len2);i++,j++){
        if(vecMp[id][i].flag==1){
           if(vecMp[id][i].content==vecTmp[j].content)continue;
           else return false;
        }
        else if(vecMp[id][i].flag==2){
           if(vecTmp[j].flag==6){
              tmpS=vecTmp[j].content;
              lenS=tmpS.size();k=0;
              while(k<lenS&&(tmpS[k]=='0'))k++;
              ans.push_back(tmpS.substr(k));
              continue;
           }
           else return false;
        }
        else if(vecMp[id][i].flag==3){
            ans.push_back(quS.substr(vecTmp[j].pos));
            i=len1;j=len2;
            break;
        }
        else if(vecMp[id][i].flag==4){
            ans.push_back(vecTmp[j].content);continue;
        }
    }
    if(i>=len1&&(j>=len2));
    else return false;
    printf("%s",name[id].c_str());
    len1=ans.size();
    for (i=0;i<len1;i++)printf(" %s",ans[i].c_str());
    printf("\n");
    return true;
}
void deal(){
    int i;
    for (i=1;i<=n;i++)if(match(i))break;
    if(i>n)printf("404\n");
}
void debug1(){
    int i,j,lenV;
    for(i=1;i<=n;i++){
        lenV=vecMp[i].size();
        for (j=0;j<lenV;j++){
            if(vecMp[i][j].flag==1)printf("str:%s ",vecMp[i][j].content.c_str());
            else if(vecMp[i][j].flag==2)printf("num:%s ",vecMp[i][j].content.c_str());
            else if(vecMp[i][j].flag==3)printf("<path> ");
            else if(vecMp[i][j].flag==-1)printf("<str> ");
            else if(vecMp[i][j].flag==-2)printf("<int> ");
        }printf("\n");
    }
}
void debug2(){
    int i,lenV;
    lenV=vecTmp.size();
    for (i=0;i<lenV;i++){
        if(vecTmp[i].flag==1)printf("str:%s ",vecTmp[i].content.c_str());
        else if(vecTmp[i].flag==2)printf("int:%s ",vecTmp[i].content.c_str());
    }printf("\n");
}
int main(){
    int i;
    string s;
    while(cin>>n>>m){
        init();
        for (i=1;i<=n;i++){
            cin>>s>>name[i];
            deal_1(i,s);
        }
        while(m--){
            cin>>quS;
            if(!check(quS)){
                printf("404\n");continue;
            }
            deal_2(quS);
            deal();
        }
    }
    return 0;
}

标签:return,URL,true,lenS,int,flag,vecMp,CCF,201803
来源: https://blog.csdn.net/qq_39507939/article/details/115032683

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有