TOJ 1593. URLs(字符串处理)

TOJ 1593. URLs(字符串处理)

一个不是很难但是很麻烦的字符串处理问题。我记得在上学期就看过这个题,觉得太麻烦就没做。
今天终于搞定它了,而且我觉得代码在AC里也算短的了。
大意是给一个域名,然后找到它的什么协议,一级域名之类的。
Samble Input :
3
ftp://acm.baylor.edu:1234/pub/staff/mr-p
http://www.informatik.uni-ulm.de/acm
gopher://veryold.edu
Sample Output:
 
URL #1
Protocol = ftp
Host     = acm.baylor.edu
Port     = 1234
Path     = pub/staff/mr-p
URL #2
Protocol = http
Host     = www.informatik.uni-ulm.de
Port     = <default>
Path     = acm
URL #3
Protocol = gopher
Host     = veryold.edu
Port     = <default>
Path     = <default>
下面是代码:

        
        
        
        
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     int i,j,k,m,n,len,key;
 7     string a,a1,a2,a3,a4;
 8     cin>>n;
 9     for(j=1;j<=n;j++){
10         cin>>a;
11         a1=a2=a3=a4="<default>";        //事先将所有字符串标记为" default "
12         len=a.length();
13         for(i=0;i<len;i++)
14             if(a[i]==':'){              //一旦遇到':'就跳出
15                 key=i;
16                 a1=a.substr(0,key);      // a1是协议名称
17                 break;
18             }
19         for(i=key+3;i<len;i++){
20             if(a[i]==':'||a[i]=='/')      //二级域名遇到':' 或者'/' 就停止
21                 break;
22             else
23                 continue;
24         }
25         if(key+3<len)
26         a2=a.substr(key+3,i-key-3);         
27         key=i; m=1;
28         for(i=key;i<len;i++){            //k 用来表示起始的位置
29             if(isdigit(a[i])){
30                 if(m){ k=i;m=0; } 
31                 continue;
32             }
33             else if(a[i]=='/')           //遇到'/'跳出
34                 break;
35         }                               // 如果存在三级域名,则赋值
36         if(i!=key)
37             a3=a.substr(k,i-k);
38         key=i+1;
39         if(key<len)a4=a.substr(key,len-key);    //剩下的是a4
40         cout<<"URL #"<<j<<endl;
41         cout<<"Protocol = "<<a1<<endl<<"Host     = "<<a2<<endl<<"Port     = "<<a3<<endl<<"Path     = "<<a4<<endl;
42         cout<<endl;
43     }
44 
45 }

你可能感兴趣的:(TOJ 1593. URLs(字符串处理))