码迷,mamicode.com
首页 > 其他好文 > 详细

uva 755 - 487--3279

时间:2016-01-26 18:17:38      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cctype>
 7 using namespace std;
 8 
 9 const char kTable[] = "2223334445556667Q77888999Z";
10 int main()
11 {
12     int T;
13     cin >> T;
14     bool first_output(true);
15     while(T--)
16     {
17         int n; cin >> n;
18         map<string, int> r;
19         for(int i = 1; i <= n; i++)
20         {
21             string t; cin >> t;
22             t.erase(remove(t.begin(), t.end(), -), t.end());
23             for(int j = 0; j < t.size(); j++)
24                 if(!isdigit(t[j]))
25                     t[j] = kTable[t[j]- A];
26             t.insert(3, "-");
27             r[t]++;
28         }
29         if(first_output)
30             first_output = false;
31         else cout << endl;
32         bool found(false);
33         for(map<string, int>::iterator it = r.begin(); it != r.end(); it++)
34             if( it->second > 1)
35             {
36                 cout << it->first << " " << it->second << endl;
37                 found = true;
38             }
39         if(!found) cout << "No duplicates." << endl;
40     }
41     return 0;
42 }

 

另一种解法:

先將每一種不同格式的電話號碼全部換成7位數整數,
利用一個hash紀錄每一種電話號碼的出現的次數,
將出現兩次以上的電話號碼紀錄到一個陣列裡面,
再利用quicksort將這個陣列以電話號碼來排序,
最後從頭將電話號碼及其出現的次數輸出來即可。

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<ctype.h>
  4 
  5 void quicksort( int start, int end, int array[] )
  6 {
  7     if( start < end )
  8     {
  9         int change = start;
 10         int i;
 11         int temp;
 12         for( i = start+1 ; i < end ; i++ )
 13             if( array[i] < array[start] )
 14             {
 15                 change++;
 16 
 17                 temp = array[i];
 18                 array[i] = array[change];
 19                 array[change] = temp;
 20             }
 21 
 22         temp = array[start];
 23         array[start] = array[change];
 24         array[change] = temp;
 25 
 26         quicksort( start, change, array );
 27         quicksort( change+1, end, array );
 28     }
 29 }
 30 
 31 int hash_numbers[10000005] = {0};
 32 
 33 int main()
 34 {
 35     int datasets;
 36     int blank;
 37     while( scanf( "%d", &datasets ) != EOF )
 38     {
 39         blank = 0 ;
 40         int numbers;
 41 
 42         while( datasets-- )
 43         {
 44             scanf( "%d", &numbers );
 45             getchar();
 46 
 47             int i;
 48             memset( hash_numbers, 0, sizeof(hash_numbers) );
 49             int output[100005] = {0};
 50             int output_saved = 0;
 51             for( i = 0 ; i < numbers ; i++ )
 52             {
 53                 char tempnum[1005];
 54                 gets( tempnum );
 55 
 56                 int templen = strlen( tempnum );
 57                 int tempnumint = 0;
 58                 int j;
 59                 for( j = 0 ; j < templen ; j++ )
 60                 {
 61                     if( isalnum( tempnum[j] ) )
 62                     {
 63                         tempnumint *= 10;
 64                         if( isdigit( tempnum[j] ) )
 65                             tempnumint += (int)(tempnum[j] - 0);
 66                         else
 67                         {
 68                             switch( tempnum[j] )
 69                             {
 70                                 case A: case B: case C:
 71                                     tempnumint += 2;
 72                                     break;
 73                                 case D: case E: case F:
 74                                     tempnumint += 3;
 75                                     break;
 76                                 case G: case H: case I:
 77                                     tempnumint += 4;
 78                                     break;
 79                                 case J: case K: case L:
 80                                     tempnumint += 5;
 81                                     break;
 82                                 case M: case N: case O:
 83                                     tempnumint += 6;
 84                                     break;
 85                                 case P: case R: case S:
 86                                     tempnumint += 7;
 87                                     break;
 88                                 case T: case U: case V:
 89                                     tempnumint += 8;
 90                                     break;
 91                                 case W: case X: case Y:
 92                                     tempnumint += 9;
 93                                     break;
 94                             }
 95                         }
 96                     }
 97                 }
 98                 hash_numbers[tempnumint]++;
 99                 if( hash_numbers[tempnumint] == 2 )
100                     output[output_saved++] = tempnumint;
101             }
102             quicksort( 0, output_saved, output );
103 
104             if( blank )
105                 printf( "\n" );
106             blank = 1;
107 
108             for( i = 0 ; i < output_saved ; i++ )
109                 printf( "%03d-%04d %d\n", output[i]/10000, output[i]%10000, hash_numbers[output[i]] );
110             if( output_saved == 0 )
111                 printf( "No duplicates.\n" );
112         }
113     }
114     return 0;
115 }

 

uva 755 - 487--3279

标签:

原文地址:http://www.cnblogs.com/aze-003/p/5161058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!