POJ 1002 487-3279

题目链接:http://poj.org/problem?id=1002&lang=zh-CN

接替思路;先把字符串转换成数字或数字串,第一次写的代码是数字串,调用了stirng,和map 所以花时较长 1500ms,第二次用数字800ms!

第一次代码:

 1 #include <stdio.h>
 2 #include <string>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <map>
 7 using namespace std;
 8 const int max_n = 1e5+3;
 9 
10 string str[max_n], str2, str3[max_n];
11 map <string, int> p_num;
12 
13 int change(int n)
14 {
15 //      cout << n << endl;
16         p_num.clear();
17         int k = 0;
18         for (int i = 0; i < n; i ++)
19         {
20                 str[i].clear();
21                 cin >> str[i];
22                 int len = str[i].length();
23                 str2 = "";
24                 for (int j = 0; j < len; j ++)
25                 {
26                         switch (str[i][j])
27                         {
28                                 case '0':
29                                 case '1':
30                                 case '2':
31                                 case '3':
32                                 case '4':
33                                 case '5':
34                                 case '6':
35                                 case '7':
36                                 case '8':
37                                 case '9': str2 += str[i][j];
38                                             break;
39                                 case 'A':
40                                 case 'B':
41                                 case 'C': str2 += '2'; break;
42                                 case 'D':
43                                 case 'E':
44                                 case 'F': str2 += '3'; break;
45                                 case 'G':
46                                 case 'H':
47                                 case 'I': str2 += '4'; break;
48                                 case 'J':
49                                 case 'K':
50                                 case 'L': str2 += '5'; break;
51                                 case 'M': 
52                                 case 'N':
53                                 case 'O': str2 += '6'; break;
54                                 case 'P':
55                                 case 'R':
56                                 case 'S': str2 += '7'; break;
57                                 case 'T':
58                                 case 'U':
59                                 case 'V': str2 += '8'; break;
60                                 case 'W':
61                                 case 'X':
62                                 case 'Y': str2 += '9'; break;
63                         }
64                         //cout << str2 << endl;
65                 }
66                 str[i] = str2;
67                 if (p_num[str[i]]++ == 1)
68                 {
69                         //p_num[str[i]] ++;
70                         str3[k ++] = str[i];
71                 }
72 //              cout << "1 " <<p_num[str[i]] << endl;
73         }
74         return k;
75 }
76 
77 int main ()
78 {
79         int n;
80         while (~scanf ("%d", &n))
81         {
82                 int k = change(n);
83                 if (k == 0)
84                         printf ("No duplicates.
");
85                 sort(str3, str3+k);
86                 for (int i = 0; i < k; i ++)
87                 {
88                         for (int j = 0; j < 7; j ++)
89                         {
90                                 if (j == 3)
91                                         cout << "-";
92                                 cout << str3[i][j];
93                         }
94                         cout << " "<< p_num[str3[i]] << endl;
95                 }
96         }
97 }
View Code

第二次代码:

  1 // File Name: /media/文档/ACM源程序/ACM基础训练题解/1.1_排序/1103.cpp
  2 // Author: sheng
  3 // Created Time: 2013年07月20日 星期六 15时05分55秒
  4 
  5 #include <stdio.h>
  6 #include <string>
  7 #include <string.h>
  8 #include <algorithm>
  9 #include <iostream>
 10 #include<iomanip>
 11 #include <map>
 12 using namespace std;
 13 const int max_n = 1e5+3;
 14 
 15 string str ;
 16 int str3[max_n];
 17 
 18 void change(int n)
 19 {
 20     int k = 0;
 21     for (int i = 0; i < n; i ++)
 22     {
 23         str.clear();
 24         cin >> str;
 25         int len = str.length();
 26         str3[i] = 0;
 27         for (int j = 0; j < len; j ++)
 28         {
 29             if (str[j] != '-' && str[j] <= 'Z')
 30                 str3[i] *= 10;
 31             switch (str[j])
 32             {
 33                 case '0': str3[i] += 0; break;
 34                 case '1': str3[i] += 1; break;
 35                 case '2':
 36                 case 'A':
 37                 case 'B':
 38                 case 'C': str3[i] += 2; break;
 39                 case '3':
 40                 case 'D':
 41                 case 'E':
 42                 case 'F': str3[i] += 3; break;
 43                 case '4':
 44                 case 'G':
 45                 case 'H':
 46                 case 'I': str3[i] += 4; break;
 47                 case '5':
 48                 case 'J':
 49                 case 'K':
 50                 case 'L': str3[i] += 5; break;
 51                 case '6':
 52                 case 'M': 
 53                 case 'N':
 54                 case 'O': str3[i] += 6; break;
 55                 case '7':
 56                 case 'P':
 57                 case 'R':
 58                 case 'S': str3[i] += 7; break;
 59                 case '8':
 60                 case 'T':
 61                 case 'U':
 62                 case 'V': str3[i] += 8; break;
 63                 case '9':
 64                 case 'W':
 65                 case 'X':
 66                 case 'Y': str3[i] += 9; break;
 67             }
 68         }
 69     //    cout << str3[i] << endl;
 70     }
 71     return;
 72 }
 73 
 74 int main ()
 75 {
 76     int n;
 77     while (~scanf ("%d", &n))
 78     {
 79         change(n);
 80         sort(str3, str3+n);
 81         int k = 1;
 82         for (int i = 0; i < n;)
 83         {
 84             int time = 0;
 85             bool flag = false;
 86             int x = str3[i];
 87             while(x == str3[i] && i < n)
 88             {
 89                 time ++;
 90                 i ++;
 91                 if (time == 2)
 92                     flag = true;
 93             }
 94             if (flag)
 95             {
 96                 k = 0;
 97                 cout<<setfill('0')<<setw(3)<<str3[i-1]/10000;  
 98                 cout<<'-';  
 99                 cout<<setfill('0')<<setw(4)<<str3[i-1]%10000;  
100                 cout<<' '<< time <<endl;  
101                 flag = false;
102                 time = 0;
103             }
104         }
105         if ( k )
106             printf ("No duplicates.
");
107     }
108     return 0;
109 }
View Code
原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3202975.html