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平方英里,以初始水岸线为 ...
随机推荐
- iOS 文件读写
#import <Foundation/Foundation.h> @interface Utils : NSObject +(void) writeFile:(NSString *) f ...
- 端午小长假--前端基础学起来04CSS选择器
定义: 选择器{ 样式: } 选择器指明{}中的样式的作用对象,即作用于网页中的哪些元素 <head><meta http-equiv="Content-Type" ...
- checkbox的全选、反选、删除(适配器)
package com.example.adapter; import java.util.List; import com.example.ay.R;import com.example.vo.Fl ...
- Linux下进程的建立
Linux下进程的建立 我们都知道,进程就是正在执行的程序.而在Linux中,可以使用一个进程来创建另外一个进程.这样的话,Linux的进程的组织结构其实有点像Linux目录树,是个层次结构的,可以使 ...
- eclipse安卓模拟器窗口大小调整
引自百度经验的链接: http://jingyan.baidu.com/article/3aed632e18c7e97011809161.html
- ABBYY
ABBYY FineReader Engine泰比OCR文字识别控件移动版 产品功能:OMR识别控件 平台: 开发商:ABBYY泰比科技 版本:产品介绍:手机识别的高品质和精度 泰比( ...
- paramiko堡垒机、线程及锁
1.使用paramiko实现ssh连接和scp拷贝 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 1.1 SSHClient 用于连接远 ...
- sql 如何把查询得到的结果如何放入一个新表中
如何把这个查询到的结果放到一张新表中? 2014-03-13 15:26 提问者采纳 表已经存在:insert into 表名 (列名1... 列名n) select 列名1....列名n f ...
- C# 线程问题
一:概述和概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线程&quo ...
- C# virtual和abstract的
virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义. 它们有一个共同点:如果用来修饰方法,前面必须添加public,要不然就会出现编译错误:虚拟方法或抽象方法是不能 ...