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
第 2 行至第 n+1 行按匹配的先后顺序描述 URL 映射规则的配置信息。第 i+1 行包含两个字符串 pi 和 ri,其中 pi 表示 URL 匹配的规则,ri 表示这条 URL 匹配的名字。两个字符串都非空,且不包含空格字符,两者中间用一个空格字符分隔。
第 n+2 行至第 n+m+1 行描述待处理的 URL 地址。第 n+1+i 行包含一个字符串 qi,表示待处理的 URL 地址,字符串中不包含空格字符。
Output
Sample Input
/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分割开,然后匹配的时候分部分匹配,这样会方便很多。要关注最后的'/',特别处理一下。
#include <bits/stdc++.h> using namespace std; const int maxn = ; string mmap[maxn];
vector< vector<string> > ori(maxn);
vector< vector<string> > now(maxn);
bool have_tail[maxn]; int n, m; inline bool ok(char ch) {
if (ch == '/' || ch == '-' || ch == '_' || ch == '.' || islower(ch) || isupper(ch) || isdigit(ch)) return true;
return false;
} void change(vector< vector<string> > &tt, string str, int pos) {
int len = str.size();
for (int i = ; i < len; i++) {
if (str[i] == '/') str[i] = ' ';
}
stringstream ss(str);
string tmp;
while (ss >> tmp) {
tt[pos].push_back(tmp);
}
} bool match(int pnow, int pori, string &ans) {
int olen = ori[pori].size();
int nlen = now[pnow].size(); int op = , np = ; while (op < olen && np < nlen) {
if (ori[pori][op] == now[pnow][np]);
else if (ori[pori][op] == "<int>") {
for (int j = ; j < (int)now[pnow][np].size(); j++) {
if (!isdigit(now[pnow][np][j])) return false;
}
int st = ;
while (now[pnow][np][st] == '' && st < (int)now[pnow][np].size()) st++;
ans += " ";
if (st == (int)now[pnow][np].size()) ans += "";
else ans += now[pnow][np].substr(st);
}
else if (ori[pori][op] == "<str>") {
ans += " ";
ans += now[pnow][op];
}
else if (ori[pori][op] == "<path>") {
ans += " " + now[pnow][np++];
while (np < nlen) {
ans += "/" + now[pnow][np++];
}
if (have_tail[pori]) ans += "/";
return true;
}
else return false;
np++, op++;
}
if (np != nlen || op != olen) return false;
return true;
} int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &m);
memset(have_tail, false, sizeof(have_tail));
string str;
for (int i = ; i <= n; i++) {
cin >> str >> mmap[i];
int ll = str.size();
if (str[ll - ] == '/') have_tail[i] = true;
change(ori, str, i);
} string ss, ans;
for (int i = ; i <= m; i++) {
cin >> ss;
int len = ss.size(); bool tail = ss[len - ] == '/' ? true : false; bool legal = true;
for (int j = ; j < len; j++) {
if (!ok(ss[j])) {
cout << << endl;
legal = false;
break;
}
} if (legal) {
change(now, ss, i);
int j;
for (j = ; j <= n; j++) {
if (tail != have_tail[j]) {
continue;
}
ans.clear();
if (match(i, j, ans)) {
cout << mmap[j] << ans << endl;
break;
}
}
if (j == n + ) cout << << endl;;
}
}
return ;
}
CCF-201803-3-URL映射(模拟)的更多相关文章
- CCF CSP 201803-3 URL映射
转载自 https://blog.csdn.net/tigerisland45/article/details/81697594 /* CCF201803-3 URL映射 */ #include &l ...
- 【CCF】URL映射 模拟
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- CCF(URL映射:80分):字符串处理+模拟
URL映射 CCF201803-3 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- 2-字符串模拟- URL映射
问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...
- CCF 201803-3 URL映射
CCF 201803-3 URL映射 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django. ...
- url映射 ccf (Java正则表达式80分解法)
问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...
- ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)
本章将讲述ASP.NET MVC5 的路由原理,即URL映射机制. 简单点就是解释:为什么MVC在浏览器输入地址就能访问到类(或类中的方法)?这是怎么做到的?我自己可以通过.NET写出一个自己的MVC ...
- MVC 5 Web编程2 -- URL映射
ASP.NET MVC 5 Web编程2 -- URL映射(路由原理) 2015-02-12 08:50 by hangwei, 704 阅读, 5 评论, 收藏, 编辑 本章将讲述ASP.NET M ...
- urlMappings与URL映射
此配置节的作用就是往Web程序中添加URL的映射,从而达到用户访问映射后的URL(如/Page/AAA)也能访问到源URL(如/Page/PageAAA.aspx)的效果.这也是URL映射本来的作用. ...
- django中“url映射规则”和“服务端响应顺序”
1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单. 查看方式: import sys print sys.path ...
随机推荐
- js 数据类型具体分析
复习 点运算符 xxx.sss xxx是对象 sss是属性和方法.任何数据类型都是拥有属性和方法的.字符串 String var st=“hello world”.字符串的定义 ...
- Get与Post的主要区别
这里附一篇自己的简短理解 get相对于post更不安全,虽然都可以加密 get的参数会显示在浏览器地址栏中,而post的参数不会显示在浏览器地址栏中: 使用post提交的页面在点击[刷新]按钮的时候浏 ...
- Android BrocastReceiver解析
简介 BroadcastReceiver(广播接收器)是Android四大组件之一,是一个用来响应系统范围内的广播组件,可以从Android系统和其它app发送或接收广播消息,类似于发布 - 订阅设计 ...
- 枚举getClass、getDeclaringClass区别
枚举getClass.getDeclaringClass区别 1):“不含抽象方法”,所有枚举常量未重写方法,的class getClass与getDeclaringClass方法输出结果相同 cla ...
- ThinkPHP登录功能的实现方法
登陆功能是PHP程序设计中常见的功能.本文ThinkPHP实例主要完成注册成功后进入首页,并告诉你是登录用户的功能.具体实现步骤如下: 第一步:在config.php文件中加上: 完整实现代码如下: ...
- 06-HTML-表格标签
<html> <head> <title>表格标签学习</title> <meta charset="utf-8"/> ...
- 关于购物车添加按钮的动画(vue.js)
来自:https://segmentfault.com/a/1190000009294321 (侵删) git 源码地址 https://github.com/ustbhuangyi/vue-sel ...
- BZOJ2199: [Usaco2011 Jan]奶牛议会(2-SAT)
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 559 Solved: 360[Submit][Status][Discuss] Descriptio ...
- Windows 不能在本地计算机启动 OracleDBConsoleorcl的问题解决方法
解决步骤如下: 1.开始->运行cmd 2.执行 emctl start dbconsole 输入:C:\Documents and Settings\xcl>emctl start db ...
- git 入门教程之实战 git
实战 git git 是一款分布式版本控制系统,可以简单概括: 不要把鸡蛋放在一个篮子里,你的一举一动都在监视中. 实战场景 你作为某项目的其中一员或者负责人,和小伙伴们一起开发,大家既有着各自分工互 ...