ZOJ-1243(parse URL)
2015-08-01 21:51
274 查看
刷刷水题练练手速
#include <iostream> #include <string> using namespace std; struct URL{ string protocol; string host; string port; string path; }; void parseUrl(const string& s, URL& url) { size_t p = s.find("://"); url.protocol = s.substr(0, p); size_t q = s.find_first_of(":/", p+3); if(q == string::npos){ url.host = s.substr(p+3); url.port = url.path = "<default>"; return; } else url.host = s.substr(p+3, q-p-3); size_t r = q; if(s[q] == ':'){ r = s.find('/', q+1); if(r == string::npos) r = s.size(); url.port = s.substr(q+1, r-q-1); } else url.port = "<default>"; if(r < s.size()) url.path = s.substr(r+1); else url.path = "<default>"; } inline void printUrl(const URL& url) { cout << "Protocol = " << url.protocol << "\n"; cout << "Host = " << url.host << "\n"; cout << "Port = " << url.port << "\n"; cout << "Path = " << url.path << "\n"; } int main() { ios::sync_with_stdio(false); URL url; string s; int i = 1, n; for(cin >> n; i <= n; ++i){ cin >> s; parseUrl(s, url); cout << "URL #" << i << "\n"; printUrl(url); cout << "\n"; } return 0; }
相关文章推荐
- Day8.01
- 用objective-c开发的第一个mac程序(1)——依赖库的生成
- 校园暑期的操场
- C#中Using用法
- 【转】什么是全栈工程师【译】
- Tasks and Back Stack
- 设计模式-观察者模式
- Python几行代码就控制了FTP
- swift学习之函数类型
- Servlet之中文乱码问题【入门版】
- hdu 5339 Untitled
- Android开发时你遇到过什么相见恨晚的工具或网站?
- STL学习记录(十六):Queue、Priority Queue
- 【转】无后端(nobackend):前端优先的Web开发【译】
- 说反话(c++实现)
- 公司的实战mongodb Replica Sets+Sharding方案采用配置文件2
- Leetcode #228 Summary Ranges
- 线性判别分析(Linear Discriminant Analysis)
- MySQL5.6.26免安装步骤
- Oracle 学习之RMAN(十四)恢复实战--基于时间点恢复