uva 755 - 487--3279

算法是蛮简单的,不过超时了

 1 #include<iostream>
 2 #include<string>
 3 #include<stdlib.h>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<stdio.h>
 7 using namespace std;
 8 vector<string> v_number;
 9 vector<int>  v_num;
10 int main(){
11     int n;
12     cin>>n;
13     string l;
14     getchar();
15     char CmapN[100];
16     CmapN['A']=CmapN['B']=CmapN['C']='2';
17     CmapN['D']=CmapN['E']=CmapN['F']='3';
18     CmapN['G']=CmapN['H']=CmapN['I']='4';
19     CmapN['J']=CmapN['K']=CmapN['L']='5';
20     CmapN['M']=CmapN['N']=CmapN['O']='6';
21     CmapN['P']=CmapN['R']=CmapN['S']='7';
22     CmapN['T']=CmapN['U']=CmapN['V']='8';
23     CmapN['W']=CmapN['X']=CmapN['Y']='9';
24     getline(cin,l);
25     while(n--){
26         int m;
27         cin>>m;
28         getchar();
29         vector<string>::iterator it;
30         while(m--){
31             getline(cin,l);    
32             for(int i=0;i<l.size();){
33                 if(!isdigit(l[i])){
34                     if(isalpha(l[i])){
35                         l[i]=CmapN[l[i]];
36                         i++;
37                     }
38                     else
39                         l.erase(i,1);
40                 }
41                 else
42                     i++;
43             }
44             l.insert(3,"-");
45             it=find(v_number.begin(),v_number.end(),l);
46             if(it==v_number.end()){
47                 v_number.push_back(l);
48                 v_num.push_back(1);
49             }
50             else{
51                 v_num[it-v_number.begin()]+=1;
52             }
53         }
54         vector<int>::iterator itn=v_num.begin();
55         it=v_number.begin();
56         for(;itn!=v_num.end();itn++){
57             if(*itn>1){
58                 char temp=*itn+'0';
59                 (*it).push_back(temp);
60                 //v_number[it-v_number.begin()].push_back(temp);
61                 it++;
62                 continue;
63             }
64             it=v_number.erase(it);
65         }
66         if(v_number.empty()){
67             cout<<"No duplicates"<<endl;
68             v_number.clear();
69             v_num.clear();
70             continue;
71         }
72         sort(v_number.begin(),v_number.end());
73         for(int i=0;i<v_number.size();i++){
74             cout<<v_number[i].substr(0,8)<<" "<<v_number[i].substr(8)<<endl;
75         }
76         if(n>0)
77             cout<<endl;
78         v_number.clear();
79         v_num.clear();
80     }
81     system("pause");
82 }

参考了网上的想法,先排序,再输出,只要一个长度的for就可以了,确实快很多,毕竟这里的数据要100000组。

 1 #include<iostream>
 2 #include<string>
 3 #include<stdlib.h>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<stdio.h>
 7 using namespace std;
 8 vector<string> v_number;
 9 int main(){
10     int n;
11     cin>>n;
12     string l;
13     getchar();
14     char CmapN[100];
15     CmapN['A']=CmapN['B']=CmapN['C']='2';
16     CmapN['D']=CmapN['E']=CmapN['F']='3';
17     CmapN['G']=CmapN['H']=CmapN['I']='4';
18     CmapN['J']=CmapN['K']=CmapN['L']='5';
19     CmapN['M']=CmapN['N']=CmapN['O']='6';
20     CmapN['P']=CmapN['R']=CmapN['S']='7';
21     CmapN['T']=CmapN['U']=CmapN['V']='8';
22     CmapN['W']=CmapN['X']=CmapN['Y']='9';
23     getline(cin,l);
24     while(n--){
25         int m;
26         cin>>m;
27         getchar();
28         while(m--){
29             getline(cin,l);    
30             for(int i=0;i<l.size();){
31                 if(!isdigit(l[i])){
32                     if(isalpha(l[i])){
33                         l[i]=CmapN[l[i]];
34                         i++;
35                     }
36                     else
37                         l.erase(i,1);
38                 }
39                 else
40                     i++;
41             }
42             l.insert(3,"-");
43             v_number.push_back(l);
44         }
45         sort(v_number.begin(),v_number.end());
46         int num=1,flag=0,i,j;
47         for(i=0;i<v_number.size();){
48             num=1;
49             for(j=i+1;j<v_number.size();j++){
50                 if(v_number[j]==v_number[i]){
51                     num++;
52                 }
53                 else{
54                     break;
55                 }
56             }
57             if(num>1){
58                 flag=1;
59                 cout<<v_number[i]<<" "<<num<<endl;
60             }
61             i=j;
62         }
63         if(!flag)
64             cout<<"No duplicates."<<endl;
65         v_number.clear();
66         if(n>0)
67             cout<<endl;
68     }
69     //system("pause");
70 }

 

posted on 2014-01-02 19:32  云在心  阅读(289)  评论(0)    收藏  举报

导航