HD4018 URL

Parsing URL
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2614 Accepted Submission(s): 1241

Problem Description
In computing, a Uniform Resource Locator or Universal Resource Locator (URL) is a character string that specifies where a known resource is available on the Internet and the mechanism for retrieving it.
The syntax of a typical URL is:
scheme://domain:port/path?query_string#fragment_id
In this problem, the scheme, domain is required by all URL and other components are optional. That is, for example, the following are all correct urls:
http://dict.bing.com.cn/#小数点
http://www.mariowiki.com/Mushroom
https://mail.google.com/mail/?shva=1#inbox
http://en.wikipedia.org/wiki/Bowser_(character)
ftp://fs.fudan.edu.cn/
telnet://bbs.fudan.edu.cn/
http://mail.bashu.cn:8080/BsOnline/
Your task is to find the domain for all given URLs.

Input
There are multiple test cases in this problem. The first line of input contains a single integer denoting the number of test cases.
For each of test case, there is only one line contains a valid URL.

Output
For each test case, you should output the domain of the given URL.

Sample Input
3
http://dict.bing.com.cn/#小数点
http://www.mariowiki.com/Mushroom
https://mail.google.com/mail/?shva=1#inbox

Sample Output
Case #1: dict.bing.com.cn
Case #2: www.mariowiki.com
Case #3: mail.google.com

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main(int argc, char *argv[]) {
	char str[10000],ans[10000];
	int i,len,n,cas=1;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",str);
		len=strlen(str);
		for(i=0;i<len;i++)
		{
			if(str[i]=='/'&&str[i-1]=='/'&&str[i-2]==':')
			break;
		}
		int j,k=0;
		for(j = i+1;j<len;j++)
		{
			if(str[j] == '/' || str[j] ==':')
			break;
			ans[k++]=str[j];
		}
		ans[k]='\0';//此为易错点
		printf("Case #%d: %s\n",cas++,ans);
	}	
	return 0;
}

思路简答,但是关键在于’\0’,这是字符串的结尾标志。HD上把这行代码注释掉就是wrong answer

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/84677793
URL
HD
今日推荐