果然正则表达式是一个强大的工具

更短的代码....hhh

 

版本1: 正则表达式..so easy~~

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <string>
 5 #include <regex>
 6 using namespace std;
 7 const int N = 107;
 8 const string s_int = "([0-9]+)";
 9 const string s_str = "([0-9A-Za-z-_\.]+)";
10 const string s_path = "([0-9A-Za-z-_\./]+)";
11 string rule[N], na[N];
12 vector <int> p[N];  // 表示匹配项类型 1-int;2-str;3-path
13 int n, m;
14 smatch sm;
15 string _deal(string str, int k) {// 将"int"->s_int'... 变成正则表达式的情况
16     string ans;
17     for (int i = 0; i < str.size(); i++) {
18         if (str[i] == '<') {
19             string tmp; i++;
20             while (str[i] != '>')
21                 tmp.push_back(str[i++]);
22             if (tmp == "int") { ans += s_int;  p[k].push_back(1); }
23             else if (tmp == "str") { ans += s_str;  p[k].push_back(2); }
24             else { ans += s_path; p[k].push_back(3); }
25         }
26         else  ans.push_back(str[i]);
27     }
28     return ans;
29 }
30 int main()
31 {
32     cin >> n >> m;
33     for (int i = 1; i <= n; i++) {
34         string s1;
35         cin >> s1 >> na[i];
36         rule[i] = _deal(s1, i);
37     }
38     for (int i = 1; i <= m; i++) {
39         string str; cin >> str;
40         string ans = "404"; int k = 1;
41         for (; k <= n; k++) {
42             regex re(rule[k]);
43             if (regex_match(str,sm,re)) {
44                 ans = na[k];
45                 break;
46             }
47         }
48         cout << ans;
49         if (ans != "404")  
50             for (int j = 0; j < p[k].size(); j++) {
51                 if (p[k][j] == 1)     cout << " " <<stoi(sm[j+1]);
52                 else                  cout << " " << sm[j+1];
53         }
54         cout << "\n";
55     }
56     //system("pause");
57     return 0;
58 

版本2: split() 用/来分割字符串  [以前写的 好复杂啊

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 vector <string> name(107);
  4 vector < vector <string> > rule(107);
  5 vector <string> ans;
  6 bool wei[107];
  7 bool isdight (char x) {
  8   return x>='0'&&x<='9';
  9 }
 10 bool ischar (char x) {
 11   if (isdight(x) )
 12     return true; 
 13   if ( (x>='A' && x<='Z') || (x>='a' && x<='z') )
 14     return true;
 15   if ( x=='/' || x=='-' || x=='_' || x=='.')
 16     return true;
 17   return false;
 18 }
 19 vector <string> split (const string& str,const char flag=' ') {
 20   istringstream iss(str);
 21   vector <string> sv;
 22   string tmp;
 23   while (getline(iss,tmp,flag)) 
 24     sv.push_back(tmp);
 25   return sv;
 26 }
 27 bool isok (string str) {
 28   if (str[0]!='/') return 0;
 29     for (int i=0;i<str.size();i++) {
 30       if (!ischar(str[i]))
 31         return 0;
 32     }
 33   return 1;
 34 
 35 }
 36 string dtoc (int x) {
 37   string str;
 38   if (x==0) return "0";
 39   while (x) {
 40     str+=x%10+'0';
 41     x/=10;
 42   }
 43   reverse(str.begin(),str.end());
 44   return str;
 45 }
 46 bool ismatch (int k, string str) {
 47   bool flag=0;
 48   ans.clear();
 49   if (str[str.size()-1]=='/') flag=1; 
 50   vector <string> sv=split (str,'/');
 51 
 52   for (int i=0;i<rule[k].size();i++) {
 53     if (i>=sv.size()) return 0;
 54     int len=sv[i].size();
 55     if (rule[k][i]=="<int>") {
 56         int sum=0;
 57         for (int j=0;j<len;j++) {
 58           if (!isdight(sv[i][j])) return 0;
 59           sum=sum*10+sv[i][j]-'0';
 60         }
 61         ans.push_back(dtoc(sum));
 62     }
 63 
 64     else if (rule[k][i]=="<str>")
 65       ans.push_back(sv[i]);
 66 
 67     else if (rule[k][i]=="<path>") {
 68       string tmp;
 69       for (int k=i;k<sv.size();k++) {
 70         tmp+=sv[k];
 71         if (k==sv.size()-1&&!flag) continue;
 72         tmp+="/";
 73       }
 74       ans.push_back(tmp);
 75       return 1;
 76     }
 77 
 78     else {
 79       if (sv[i]!=rule[k][i]) return 0;
 80     }
 81 
 82   }
 83   if (rule[k].size()==sv.size()&&(flag==wei[k])) return 1; // 真的是傻逼
 84   else                           return 0;
 85 }
 86 int n,m;
 87 int main ()
 88 {
 89   cin>>n>>m; getchar();
 90   string s1,s2;
 91   for (int i=1;i<=n;i++) {
 92     cin>>s1>>s2; getchar();
 93     name[i]=s2;
 94     if (s1[s1.size()-1]=='/') wei[i]=1;// 没有考虑的地方
 95     rule[i]=split(s1,'/');
 96   }
 97   for (int i=1;i<=m;i++) {
 98     getline (cin,s1); int x=0;
 99     if (isok(s1))  {
100       for (int j=1;j<=n;j++) 
101         if (ismatch(j,s1)) {
102           x=j;
103           break;
104         }
105     }
106     if (x==0) cout<<"404"<<endl;
107     else {
108       cout<<name[x];
109       for (int j=0;j<ans.size();j++)
110         cout<<" "<<ans[j];
111       cout<<endl;
112     }
113   }
114   return 0;
115 }