标签:失败 规则 下划线 先后 必须 文字 style 表示 back
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int N = 100 + 5; 5 6 int n, m; 7 vector<string> reg[N]; 8 vector<string> reg_name; 9 10 void format_reg(int c, string & line) 11 { 12 stringstream ss(line); 13 string name; 14 ss >> line >> name; 15 reg_name.push_back(name); 16 17 for(int i = 0; i < (int)line.length(); i++) { 18 if(line[i] == ‘/‘) { 19 reg[c].push_back("/"); 20 }else { 21 if(reg[c].back() == "/") { 22 reg[c].push_back(string(&line[i], &line[i] + 1)); 23 }else { 24 string & tmp = reg[c].back(); 25 tmp += string(&line[i], &line[i] + 1); 26 } 27 } 28 } 29 } 30 31 void url_split(string &url, vector<string> &urls) 32 { 33 for(int i = 0; i < url.length(); i++) { 34 if(url[i] == ‘/‘) { 35 urls.push_back("/"); 36 }else { 37 if(urls.back() == "/") { 38 urls.push_back(string(&url[i], &url[i] + 1)); 39 }else { 40 string & tmp = urls.back(); 41 tmp += string(&url[i], &url[i] + 1); 42 } 43 } 44 } 45 } 46 bool is_int(string & s) 47 { 48 for(int i = s.length() - 1; i >= 0; i--) { 49 if(!isdigit(s[i])) return false; 50 } 51 return true; 52 } 53 54 bool is_equal(string & regn, vector<string> & urls, vector<string> & param) 55 { 56 57 if(urls.size() == 0) return false; 58 if(regn == urls.front()) { 59 urls.erase(urls.begin()); 60 return true; 61 }else if(regn == "<path>") { 62 string tmp = ""; 63 for(int j = 0; j < (int)urls.size(); j++) { 64 tmp += urls[j]; 65 } 66 param.push_back(tmp); 67 urls.clear(); 68 return true; 69 }else if(regn == "<int>") { 70 if(is_int(urls.front())) { 71 param.push_back(urls.front()); 72 urls.erase(urls.begin()); 73 return true; 74 } 75 return false; 76 }else if(regn == "<str>") { 77 param.push_back(urls.front()); 78 urls.erase(urls.begin()); 79 return true; 80 } 81 return false; 82 } 83 84 bool match(const int &c, string url, vector<string> & param) 85 { 86 vector<string> urls; 87 stringstream ss(url); 88 url_split(url, urls); 89 90 for(size_t i = 0; i < reg[c].size(); i++) { 91 if(!is_equal(reg[c][i], urls, param)) return false; 92 if(urls.size() == 0 && i == reg[c].size() - 1) return true; 93 } 94 95 return false; 96 } 97 98 void print_reg() 99 { 100 for(int i = 0; i < n; i++) { 101 for(auto & tmp : reg[i]) cout << tmp << " "; 102 cout << endl; 103 } 104 } 105 106 int get_result(string & url, vector<string> & param) 107 { 108 109 for(int i = 0; i < n; i++) { 110 param.clear(); 111 //cout << "---------------------------" << endl; 112 if(match(i, url, param)) return i; 113 } 114 return -1; 115 } 116 117 int main() 118 { 119 cin >> n >> m; 120 cin.ignore(); 121 string line; 122 for(int i = 0; i < n; i++) { 123 getline(cin, line); 124 format_reg(i, line); 125 } 126 //print_reg(); 127 128 vector<string> param; 129 for(int i = 0; i < m; i++) { 130 getline(cin, line); 131 int ans = get_result(line, param); 132 if(ans == -1) { 133 cout << 404 << endl; 134 }else { 135 cout << reg_name[ans]<< " "; 136 for(int i = 0; i < (int)param.size(); i++) { 137 if(is_int(param[i])) { 138 int ans = atoi(param[i].c_str()); 139 cout << ans << " "; 140 }else 141 cout << param[i] << " "; 142 } 143 cout << endl; 144 } 145 } 146 }
标签:失败 规则 下划线 先后 必须 文字 style 表示 back
原文地址:https://www.cnblogs.com/Pretty9/p/11511075.html