ZOJ 1243 URLs
/*
In the early nineties, the World Wide Web (WWW) was invented. Nowadays, most people think that the WWW simply consists of all the pretty (or not so pretty) HTML-pages that you can read with your WWW browser. But back then, one of the main intentions behind the design of the WWW was to unify several existing communication protocols.
Then (and even now), information on the Internet was available via a multitude of channels: FTP, HTTP, E-Mail, News, Gopher, and many more. Thanks to the WWW, all these services can now be uniformly addressed via URLs (Uniform Resource Locators). The syntax of URLs is defined in the Internet standard RFC 1738. For our problem, we consider a simplified version of the syntax, which is as follows:
“://” [ ":" ] [ "/" ]
The square brackets [] mean that the enclosed string is optional and may or may not appear. Examples of URLs are the following:
http://www.informatik.uni-ulm.de/acm
ftp://acm.baylor.edu:1234/pub/staff/mr-p
gopher://veryold.edu
More specifically,
is always one of http, ftp or gopher.
is a string consisting of alphabetic (a-z, A-Z) or numeric (0-9) characters and points (.).
is a positive integer, smaller than 65536.
is a string that contains no spaces.
You are to write a program that parses an URL into its components.
Input
The input starts with a line containing a single integer n, the number of URLs in the input. The following n lines contain one URL each, in the format described above. The URLs will consist of at most 60 characters each.
Output
For each URL in the input first print the number of the URL, as shown in the sample output. Then print four lines, stating the protocol, host, port and path specified by the URL. If the port and/or path are not given in the URL, print the stringinstead. Adhere to the format shown in the sample output.
Print a blank line after each test case.
Sample Input
3
ftp://acm.baylor.edu:1234/pub/staff/mr-p
http://www.informatik.uni-ulm.de/acm
gopher://veryold.edu
Sample Output
URL #1
Protocol = ftp
Host = acm.baylor.edu
Port = 1234
Path = pub/staff/mr-p
URL #2
Protocol = http
Host = www.informatik.uni-ulm.de
Port =
Path = acm
URL #3
Protocol = gopher
Host = veryold.edu
Port =
Path =
*/
#include <string>
#include <iostream> using namespace std; int main()
{
int n; string s; cin >> n;
for(int i = 1; i <= n; i++)
{
int p;
cin >> s; p = s.find("://"); string protocol = s.substr(0, p);
string host = s.substr(p + 3, s.size());
string port;
string path; p = host.find('/');
if(p != string::npos)
{
path = host.substr(p + 1, host.size());
host = host.substr(0, p);
} p = host.find(':');
if(p != string::npos)
{
int k;
for(k = p + 1; isdigit(host[k]); k++)
port += host[k];
host = host.substr(0, p);
} cout << "URL #" << i << endl
<< "Protocol = " << protocol << endl
<< "Host = " << host << endl
<< "Port = " << (port == "" ? "<default>" : port) << endl
<< "Path = " << (path == "" ? "<default>" : path) << endl << endl;
} return 0;
}
ZOJ 1243 URLs的更多相关文章
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- Django基础,Day2 - 编写urls,views,models
编写views views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去. polls/views.py: from django.h ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
随机推荐
- Android事件处理--读书笔记
2013-12-28 17:57:17 1. Android提供了两种方式的事件处理:基于回调和基于监听的事件处理: 1.1 对基于监听的事件处理而言,主要做法是为Android界面组件绑定特定的事件 ...
- java.lang.InstantiationException
java.lang.InstantiationException 出现这种异常的原因通常情况下是由于要实例化的对象是一个接口或者是抽象类等无法被实例化的类.
- CSS引入外部字体
@font-face { font-family: '综艺体'; font-style: normal; font-weight: normal; src: url(../cs ...
- IOS 中 NSArray
今天在做 cordova 关于处理未读条数的插件时,需要在js中传入 int 型. 但是发现所有插件 里面的参数信息都是封装在NSArry对象里面. 因此又突然想直接在NSArry获取信息的想法 ...
- innerHTML..innerText..textContent
/* * innerText和textContent 都是设置文字内容的,如果设置的内容当中有标签,也会直接的以文本的方式显示(标签的<>都会按照转义的方式进行解析) * innerTex ...
- mycat启动后,用Navicat Premium 连接报 "2013"
最近在学习mycat,启动后,用Navicat Premium 连接报 "2013" Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...
- UID 修改 & UID 锁死修复
首先是UID修改的问题,只要卡是UID卡,就都可以修改UID,首先读卡器连接电脑,卡片放到读卡器上. 然后我们要用一个工具,UID207.打开UID207.exe,点Initialize,初始化. 然 ...
- UITouch的用法
UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得. //这四个方法是UIResponder中得方法 // Generally, all responders wh ...
- "This connection is untrusted" - Firefox error message
Error Messages I am receiving the following error message in Firefox: After selecting Cancel to clos ...
- XML JSON解析--基本功能
一,json的解析 json文件: {"code": "cn","cities": [{"name": " ...