CCF (URL mapping: 80): String Processing Simulation +

URL mapping

CCF201803-3

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<sstream>
#include<map>
#include<queue>
using namespace std;
const int maxn=101;
const int maxm=101;
string s[maxn];
// 字符串 <str>:用于匹配一段字符串,注意字符串里不能包含斜杠。例如,abcde0123。
// 整数 <int>:用于匹配一个不带符号的整数,全部由阿拉伯数字组成。例如,01234。
// 路径 <path>:用于匹配一段字符串,字符串可以包含斜杠。例如,abcd/0123/。
struct node{
    int id;//1-int,2-str,3-path
    string s;
};
vector<node> ve;
int n,m;
map<string,string>ma;
bool legal(char c){
    return (c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='.'||c=='_'||c=='-';
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        string name;
        cin>>s[i]>>name;
        ma[s[i]]=name;
    }
    for(int i=0;i<m;i++){
        string t;
        cin>>t;
        bool flag=false;
        for(int j=0;j<n;j++){
            ve.clear();
            string now=s[j];
            bool flagsub=true;
            int ks,kt;
            for(ks=0,kt=0;ks<now.length()&&kt<t.length();){//k表示s[i]
                if(now[ks]=='/'&&t[kt]=='/'){
                    ks++,kt++;
                    continue;
                }
                if(now[ks]=='<'){
                    string temp="";
                    while(now[++ks]!='>'){
                        temp+=now[ks];
                    }
                    ks++;//--------------
                    if(temp=="int"){
                        string ts="";
                        ts+=t[kt];
                        while(t[++kt]!='/'){
                            ts+=t[kt];
                            if(t[kt]>'9'||t[kt]<'0'){
                                flagsub=false;
                                break;
                            }
                        }
                        ve.push_back(node{1,ts});
                    }else if(temp=="str"){
                        string ts="";
                        ts+=t[kt];
                        while(t[++kt]!='/'){
                            ts+=t[kt];
                            if(!legal(t[kt])){
                                flagsub=false;
                                break;
                            }
                        }
                        ve.push_back(node{2,ts});
                    }else if(temp=="path"){//是路径的话
                        string ts="";
                        ts+=t[kt];
                        while((++kt)!=t.length()){
                            ts+=t[kt];
                            if(!legal(t[kt])&&t[kt]!='/'){
                                flagsub=false;
                                break;
                            }
                        }
                        ve.push_back(node{3,ts});
                    }
                    continue;
                }
                if(now[ks]==t[kt]){
                    ks++,kt++;
                    continue;
                }
                if(now[ks]!=t[kt]){
                    flagsub=false;
                    break;
                }
            }
            if(ks<now.length()||kt<t.length()){
                flagsub=false;
            }
            if(flagsub){
                flag=true;
                cout<<ma[now];
                for(int k=0;k<ve.size();k++){
                    if(ve[k].id==1){//数字
                        cout<<" ";
                        int ksb=0;
                        if(ve[k].s[0]=='0')
                            ksb++;
                        for(;ksb<ve[k].s.length();ksb++){
                            cout<<ve[k].s[ksb];
                        }
                    }else
                        cout<<" "<<ve[k].s;
                }
                cout<<endl;
                break;
            }
        }
        if(!flag){
            cout<<404<<endl;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/GarrettWale/p/11443552.html