• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

【周全考虑】CodeForces 245B——Internet Address

来源:点击打开链接

看上去很简单的一道题,可是错的次数却不少。

题目要求是将一串字母转化成网址——形如格式http(ftp)://xxx.ru/xxxx的样子,看上去很简单,可是还是很容易出错。

刚开始找的时候是按照寻找第一组http/ftp,然后寻找第一个ru,构成网址,但是报错了,反例如下:httpruc

所以不能寻找第一个网址,也就是说尽量避免.ru之前没有东西,这样是不合法 的。然后注意http是四个字符,ftp只有三个字符,所以不能固定。。

 

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string tar,res;
	string tarstack;
	int propos=0,ctpos=0;
	cin>>tar;
	if(tar[0]=='h')
	{
		tarstack="http";
	}
	if(tar[0]=='f')
	{
		tarstack="ftp";
	}
	ctpos=tar.find("ru",tarstack.length()+1);  //5是不行的,惯性思维不可 
	if(tar[0]=='h')
	{
		cout<<tarstack<<"://";
		for(int i=4;i<ctpos;i++)
		{
			cout<<tar[i];
		}
		cout<<".ru";
		if(ctpos+2==tar.length())
			cout<<endl;
		else
		{
			cout<<"/";
			for(int i=ctpos+2;i<tar.length();i++)
			{
				cout<<tar[i];
			}
			cout<<endl;
		}
		
	}
	else if(tar[0]=='f')
	{
		cout<<tarstack<<"://";
		for(int i=3;i<ctpos;i++)
		{
			cout<<tar[i];
		}
		cout<<".ru";
		if(ctpos+2==tar.length())
			cout<<endl;
		else
		{
			cout<<"/";
			for(int i=ctpos+2;i<tar.length();i++)
			{
				cout<<tar[i];
			}
			cout<<endl;
		}
	}
	return 0;
	
}
//范例:httpruhhphhhpuhruruhhpruhhphruhhru 


 

 

posted @ 2013-08-16 18:02  Class Xman  阅读(184)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3