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平方英里,以初始水岸线为 ...
随机推荐
- Windows多线程编程总结
1 内核对象 1 .1 内核对象的概念 内核对象是内核分配的一个内存块,这种内存块是一个数据结构,表示内核对象的各种特征.并且只能由内核来访问.应用程序若需要访问内核对象,需要通过操作系统提供的函数来 ...
- 一模 (1) day2
第一题:(水题) 题目大意:就是给出扫雷的图,然后统计每个九宫格的雷的个数. 解题过程: 1.好久没做这样的水题了.直接模拟水过.. 第二题: 题目大意:给出一个长度小于1000的数k,要求一个尽可能 ...
- NOIP 2003解题报告
第一题(神经网络): 题目大意,给出一些点,每个点都有2个值,c和u,给出一些有向边,权值为w.入度为0的点的c已知,其它点的c未知,每个入度不为0的点node的c等于sum(c[k]*w[k][no ...
- Java与.NET DES加密解密互转
上代码: Java代码: import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKe ...
- redis 详解
什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) Reids的特点 Redis本质上是一个Key-Value类型的内存数据库, ...
- 关键字const
const关键字常和指针一起使用. 1,const给读代码的人传达非常有用的信息.比如一个函数的参数是const char *,你在调用这个函数时就可以放心地传给它char *或const char ...
- STM32之延时秒,毫秒,微秒
#include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...
- 生成1~n的全排列
输入正整数n,输出n的全排列. 样例输入1: 3 样例输出1: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 分析: 按字典序从小到大的顺序输出所有的排列. (字典序:两个序 ...
- 块状元素和内联元素 【inline block】
// 9) { colorRandom += colorArray[randomV - 10]; } else { colorRandom += randomV; } } currentEle.css ...
- droidbox官网
droidbox官网,droidbox已经移植到github上了 https://github.com/pjlantz/droidbox