#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std; const char kTable[] = "2223334445556667Q77888999Z";
int main()
{
int T;
cin >> T;
bool first_output(true);
while(T--)
{
int n; cin >> n;
map<string, int> r;
for(int i = ; i <= n; i++)
{
string t; cin >> t;
t.erase(remove(t.begin(), t.end(), '-'), t.end());
for(int j = ; j < t.size(); j++)
if(!isdigit(t[j]))
t[j] = kTable[t[j]- 'A'];
t.insert(, "-");
r[t]++;
}
if(first_output)
first_output = false;
else cout << endl;
bool found(false);
for(map<string, int>::iterator it = r.begin(); it != r.end(); it++)
if( it->second > )
{
cout << it->first << " " << it->second << endl;
found = true;
}
if(!found) cout << "No duplicates." << endl;
}
return ;
}
另一种解法:
先將每一種不同格式的電話號碼全部換成7位數整數,
利用一個hash紀錄每一種電話號碼的出現的次數,
將出現兩次以上的電話號碼紀錄到一個陣列裡面,
再利用quicksort將這個陣列以電話號碼來排序,
最後從頭將電話號碼及其出現的次數輸出來即可。
#include<stdio.h>
#include<string.h>
#include<ctype.h> void quicksort( int start, int end, int array[] )
{
if( start < end )
{
int change = start;
int i;
int temp;
for( i = start+ ; i < end ; i++ )
if( array[i] < array[start] )
{
change++; temp = array[i];
array[i] = array[change];
array[change] = temp;
} temp = array[start];
array[start] = array[change];
array[change] = temp; quicksort( start, change, array );
quicksort( change+, end, array );
}
} int hash_numbers[] = {}; int main()
{
int datasets;
int blank;
while( scanf( "%d", &datasets ) != EOF )
{
blank = ;
int numbers; while( datasets-- )
{
scanf( "%d", &numbers );
getchar(); int i;
memset( hash_numbers, , sizeof(hash_numbers) );
int output[] = {};
int output_saved = ;
for( i = ; i < numbers ; i++ )
{
char tempnum[];
gets( tempnum ); int templen = strlen( tempnum );
int tempnumint = ;
int j;
for( j = ; j < templen ; j++ )
{
if( isalnum( tempnum[j] ) )
{
tempnumint *= ;
if( isdigit( tempnum[j] ) )
tempnumint += (int)(tempnum[j] - '');
else
{
switch( tempnum[j] )
{
case 'A': case 'B': case 'C':
tempnumint += ;
break;
case 'D': case 'E': case 'F':
tempnumint += ;
break;
case 'G': case 'H': case 'I':
tempnumint += ;
break;
case 'J': case 'K': case 'L':
tempnumint += ;
break;
case 'M': case 'N': case 'O':
tempnumint += ;
break;
case 'P': case 'R': case 'S':
tempnumint += ;
break;
case 'T': case 'U': case 'V':
tempnumint += ;
break;
case 'W': case 'X': case 'Y':
tempnumint += ;
break;
}
}
}
}
hash_numbers[tempnumint]++;
if( hash_numbers[tempnumint] == )
output[output_saved++] = tempnumint;
}
quicksort( , output_saved, output ); if( blank )
printf( "\n" );
blank = ; for( i = ; i < output_saved ; i++ )
printf( "%03d-%04d %d\n", output[i]/, output[i]%, hash_numbers[output[i]] );
if( output_saved == )
printf( "No duplicates.\n" );
}
}
return ;
}