zoukankan      html  css  js  c++  java
  • CCF-201803-3-URL映射(模拟)

    Problem CCF-201803-3-URL映射

    Time Limit: 1000 mSec

    Problem Description

    URL 映射是诸如 Django、Ruby on Rails 等网页框架 (web frameworks) 的一个重要组件。对于从浏览器发来的 HTTP 请求,URL 映射模块会解析请求中的 URL 地址,并将其分派给相应的处理代码。现在,请你来实现一个简单的 URL 映射功能。
      本题中 URL 映射功能的配置由若干条 URL 映射规则组成。当一个请求到达时,URL 映射功能会将请求中的 URL 地址按照配置的先后顺序逐一与这些规则进行匹配。当遇到第一条完全匹配的规则时,匹配成功,得到匹配的规则以及匹配的参数。若不能匹配任何一条规则,则匹配失败。
      本题输入的 URL 地址是以斜杠 / 作为分隔符的路径,保证以斜杠开头。其他合法字符还包括大小写英文字母、阿拉伯数字、减号 -、下划线 _ 和小数点 .。例如,/person/123/ 是一个合法的 URL 地址,而 /person/123? 则不合法(存在不合法的字符问号 ?)。另外,英文字母区分大小写,因此 /case/ 和 /CAse/ 是不同的 URL 地址。
      对于 URL 映射规则,同样是以斜杠开始。除了可以是正常的 URL 地址外,还可以包含参数,有以下 3 种:
      字符串 <str>:用于匹配一段字符串,注意字符串里不能包含斜杠。例如,abcde0123。
      整数 <int>:用于匹配一个不带符号的整数,全部由阿拉伯数字组成。例如,01234。
      路径 <path>:用于匹配一段字符串,字符串可以包含斜杠。例如,abcd/0123/。
      以上 3 种参数都必须匹配非空的字符串。简便起见,题目规定规则中 <str> 和 <int> 前面一定是斜杠,后面要么是斜杠,要么是规则的结束(也就是该参数是规则的最后一部分)。而 <path> 的前面一定是斜杠,后面一定是规则的结束。无论是 URL 地址还是规则,都不会出现连续的斜杠。

    Input

      输入第一行是两个正整数 nm,分别表示 URL 映射的规则条数和待处理的 URL 地址个数,中间用一个空格字符分隔。
      第 2 行至第 n+1 行按匹配的先后顺序描述 URL 映射规则的配置信息。第 i+1 行包含两个字符串 piri,其中 pi 表示 URL 匹配的规则,ri 表示这条 URL 匹配的名字。两个字符串都非空,且不包含空格字符,两者中间用一个空格字符分隔。
      第 n+2 行至第 n+m+1 行描述待处理的 URL 地址。第 n+1+i 行包含一个字符串 qi,表示待处理的 URL 地址,字符串中不包含空格字符。

     Output

    输入共 m 行,第 i 行表示 qi 的匹配结果。如果匹配成功,设匹配了规则 pj ,则输出对应的 rj。同时,如果规则中有参数,则在同一行内依次输出匹配后的参数。注意整数参数输出时要把前导零去掉。相邻两项之间用一个空格字符分隔。如果匹配失败,则输出 404。

     Sample Input

    5 4
    /articles/2003/ special_case_2003
    /articles/<int>/ year_archive
    /articles/<int>/<int>/ month_archive
    /articles/<int>/<int>/<str>/ article_detail
    /static/<path> static_serve
    /articles/2004/
    /articles/1985/09/aloha/
    /articles/hello/
    /static/js/jquery.js
     

     Sample Output

    year_archive 2004
    article_detail 1985 9 aloha
    404
    static_serve js/jquery.js

    题解:很坑的CCF第三题,模拟题,比较繁琐,关键一步就是把 '/' 换成 ' ' ,然后用stringstream分割开,然后匹配的时候分部分匹配,这样会方便很多。要关注最后的'/',特别处理一下。

      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 const int maxn = 105;
      6 
      7 string mmap[maxn];
      8 vector< vector<string> > ori(maxn);
      9 vector< vector<string> > now(maxn);
     10 bool have_tail[maxn];
     11 
     12 int n, m;
     13 
     14 inline bool ok(char ch) {
     15     if (ch == '/' || ch == '-' || ch == '_' || ch == '.' || islower(ch) || isupper(ch) || isdigit(ch)) return true;
     16     return false;
     17 }
     18 
     19 void change(vector< vector<string> > &tt, string str, int pos) {
     20     int len = str.size();
     21     for (int i = 0; i < len; i++) {
     22         if (str[i] == '/') str[i] = ' ';
     23     }
     24     stringstream ss(str);
     25     string tmp;
     26     while (ss >> tmp) {
     27         tt[pos].push_back(tmp);
     28     }
     29 }
     30 
     31 bool match(int pnow, int pori, string &ans) {
     32     int olen = ori[pori].size();
     33     int nlen = now[pnow].size();
     34 
     35     int op = 0, np = 0;
     36 
     37     while (op < olen && np < nlen) {
     38         if (ori[pori][op] == now[pnow][np]);
     39         else if (ori[pori][op] == "<int>") {
     40             for (int j = 0; j < (int)now[pnow][np].size(); j++) {
     41                 if (!isdigit(now[pnow][np][j])) return false;
     42             }
     43             int st = 0;
     44             while (now[pnow][np][st] == '0' && st < (int)now[pnow][np].size()) st++;
     45             ans += " ";
     46             if (st == (int)now[pnow][np].size()) ans += "0";
     47             else ans += now[pnow][np].substr(st);
     48         }
     49         else if (ori[pori][op] == "<str>") {
     50             ans += " ";
     51             ans += now[pnow][op];
     52         }
     53         else if (ori[pori][op] == "<path>") {
     54             ans += " " + now[pnow][np++];
     55             while (np < nlen) {
     56                 ans += "/" + now[pnow][np++];
     57             }
     58             if (have_tail[pori]) ans += "/";
     59             return true;
     60         }
     61         else return false;
     62         np++, op++;
     63     }
     64     if (np != nlen || op != olen) return false;
     65     return true;
     66 }
     67 
     68 int main()
     69 {
     70     //freopen("input.txt", "r", stdin);
     71     scanf("%d%d", &n, &m);
     72     memset(have_tail, false, sizeof(have_tail));
     73     string str;
     74     for (int i = 1; i <= n; i++) {
     75         cin >> str >> mmap[i];
     76         int ll = str.size();
     77         if (str[ll - 1] == '/') have_tail[i] = true;
     78         change(ori, str, i);
     79     }
     80 
     81     string ss, ans;
     82     for (int i = 1; i <= m; i++) {
     83         cin >> ss;
     84         int len = ss.size();
     85 
     86         bool tail = ss[len - 1] == '/' ? true : false;
     87 
     88         bool legal = true;
     89         for (int j = 0; j < len; j++) {
     90             if (!ok(ss[j])) {
     91                 cout << 404 << endl;
     92                 legal = false;
     93                 break;
     94             }
     95         }
     96 
     97         if (legal) {
     98             change(now, ss, i);
     99             int j;
    100             for (j = 1; j <= n; j++) {
    101                 if (tail != have_tail[j]) {
    102                     continue;
    103                 }
    104                 ans.clear();
    105                 if (match(i, j, ans)) {
    106                     cout << mmap[j] << ans << endl;
    107                     break;
    108                 }
    109             }
    110             if (j == n + 1) cout << 404 << endl;;
    111         }
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    点击bindingNavigatorAddNewItem 关联的dataGridView不会新增一行
    dataGridView的使用经验
    Mybatis源码分析:Resources
    Mybatis源码分析:BaseBuilder
    反射类 Modifier类
    Mybatis源码分析:SqlSessionManager
    Mybatis源码分析:SqlSessionFactory
    Mybatis源码分析:环境设置Environment
    Mybatis源码分析:类型处理器TypeHandler
    Mybatis源码分析:MapperRegistry
  • 原文地址:https://www.cnblogs.com/npugen/p/9638191.html
Copyright © 2011-2022 走看看