1. 程式人生 > >CCF CSP URL對映

CCF CSP URL對映

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
map<string, string> dict;
vector<string> templist;
string trim(string s){
    if (s.empty()) return s;
    return s.substr(s.find_first_not_of('0'
)); } bool ismatch(string const & rule, string & url){ vector<string> paramter; int ix = 0; int p = 0; while (true){ if (ix>=url.size()) break; if (rule[p] == url[ix]){ ix++, p++; } else if (rule[p] == '<'){ p++; string
s; switch (rule[p]){ case 'p': cout<<dict[rule]; for (int i = 0; i < paramter.size(); ++i) { cout<<" "<<paramter[i]; } cout<<" "<<url.substr(ix)<<endl; return
true; case 'i': while (ix<url.size()&&isdigit(url[ix])){ s += url[ix]; ix++; } paramter.push_back(trim(s)); p += 4; break; case 's': while (ix<url.size()&&(isdigit(url[ix])||isalpha(url[ix]))){ s += url[ix]; ix++; } paramter.push_back(s); p += 4; break; default: break; } } else return false; } if (p>=rule.size()){ cout<<dict[rule]; for (int i = 0; i < paramter.size(); ++i) { cout<<" "<<paramter[i]; } cout<<endl; return true; } return false; } void solve(string & url){ vector<string>::iterator it = templist.begin(); bool isok = false; for (; it!=templist.end(); it++){ if (ismatch(*it, url)){ isok = true; break; } } if (!isok) cout<<"404"<<endl; } int main(){ // freopen("../in.txt", "r", stdin); int m, n; cin>>m>>n; string rule, name; for (int i = 0; i < m; ++i) { cin>>rule>>name; templist.push_back(rule); dict[rule] = name; } string url; for (int i = 0; i < n; ++i) { cin>>url; solve(url); } }