求公共前缀(水)、URL(水)

求公共前缀:编写程序测试一个函数,该函数查找两个单词最长的共同前缀(例如,“global”和“glossary”最长的 共同前缀是“glo”,“department”和“depart”最长的共同前缀是“depart”,“glove”和“dove”最长的共同前缀 是空字符串)。 

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string same_prefix(const string& s1, const string& s2)
{
	string s;
	auto min_size = min(s1.size(), s2.size());
	int i = 0;
	for (; i != min_size; ++i)
		if (s1[i] != s2[i])
			break;
	s = s1.substr(0, i);
	return s;
}
int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	cout << same_prefix(s1, s2) << endl;
	system("pause");
}

URL:Internet 上的可用资源可以用简单字符串来表示,被称为“统一资源定位器”(URL,Uniform / Universal Resource Locator)。 URL 的标准格式如下: 
协议类型://服务器地址(:端口号)/路径/文件名 
在应用中,我们常常需要从一条 URL 中截取远程服务器地址部分(IP 地址或域名),以便进一步处理。 请编写程序,输入为 URL,输出为远程服务器地址部分。例如,下面是 5 组输入和输出数据。 
 
输入数据示例: 输出结果示例:

http://acm.xidian.edu.cn/                   acm.xidian.edu.cn 

http://qinz.maybe.im/                         cn qinz.maybe.im

https://www.something.com:8080/p?id=0              www.something.com

ftp://ftp.anything.com/nothing.zip                    ftp.anything.com

http://127.0.0.1/a/b/c/d/e                     127.0.0.1 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s;
	getline(cin, s);
	auto start = s.find("://") + 3;
	auto end = s.find_first_of(":/", start);
	string s1 = s.substr(start, end - start);
	cout << s1 << endl;
	system("pause");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值