0%

URLs

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=243

题意: 给一个URL 找出URL的protocol、host、port、path,port和path可能不给出(即输出)。一道水题,但是一开始想法没有AC,后来改了改AC了…浪费了挺多时间

AC的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
int n;
int cnt;
void solve(){
string s;
cin>>s;
string protocal,host,port="<default>",path="<default>";
int i;
i=s.find("://");
protocal=s.substr(0,i);
s.erase(0,i+3);
i=s.find("/");
if(i!=string::npos){
host=s.substr(0,i);
path=s.substr(i+1,s.size());
s.erase(i,s.size());
}
else{
host=s.substr(0,s.size());
}
i=s.find(":");
if(i!=string::npos){
host=s.substr(0,i);
s.erase(0,i+1);
port=s;
}
cout<<"URL #"<<cnt<<endl;
cout<<"Protocol = "<<protocal<<endl;
cout<<"Host = "<<host<<endl;
cout<<"Port = "<<port<<endl;
cout<<"Path = "<<path<<endl;
}

int main()
{
scanf("%d",&n);
cnt=0;
while(n--){
cnt++;
solve();
cout<<endl;
}
return 0;
}

WA的代码,具体原因我也不知道,感觉自己思路是对的…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
int n;
int cnt;
void solve(){
string s;
cin>>s;
string protocal,host,port="<default>",path="<default>";
int i;
i=s.find("://");
protocal=s.substr(0,i);
s.erase(0,i+3);
i=s.find(":");
if(i!=string::npos){
host=s.substr(0,i);
s.erase(0,i+1);
i=s.find("/");
if(i!=string::npos){
port=s.substr(0,i);
s.erase(0,i+1);
path=s.substr(0,s.size());
}
else{
port=s.substr(0,s.size());

}
}
else{
i=s.find("/");
if(i!=string::npos){
host=s.substr(0,i);
s.erase(0,i+1);
path=s.substr(0,s.size());
}
else{
host=s.substr(0,s.size());
}
}
cout<<"URL #"<<cnt<<endl;
cout<<"Protocol = "<<protocal<<endl;
cout<<"Host = "<<host<<endl;
cout<<"Port = "<<port<<endl;
cout<<"Path = "<<path<<endl;
}

int main()
{
scanf("%d",&n);
cnt=0;
while(n--){
cnt++;
solve();
cout<<endl;
}
return 0;
}

-------------本文结束感谢您的阅读-------------