Skip to content

Instantly share code, notes, and snippets.

@jason790228
Created May 30, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason790228/8a368b63d595e9c5d36302200cf4042e to your computer and use it in GitHub Desktop.
Save jason790228/8a368b63d595e9c5d36302200cf4042e to your computer and use it in GitHub Desktop.
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
class DictionaryComparer
{
public:
enum phone_number_information
{
SEPARATE_SYMBOLS_POSITION = 3,
PHONE_NUMBER_LENGTH = 8
};
public:
std::map<char, char> m_uppercase_letters_to_phone_number_table;
std::map<std::string, int> m_dictionary;
std::vector<std::vector<std::string>> m_input;
std::vector<std::map<std::string, int>> m_output;
public:
DictionaryComparer();
public:
std::string NormalizePhoneNumberFormat(std::string phone_number);
bool IsVaildPhoneNumberFormat(std::string phone_number);
void AddDictionary(std::string phone_number);
void CompareWithDictionary();
};
using namespace std;
DictionaryComparer::DictionaryComparer()
{
m_uppercase_letters_to_phone_number_table['0'] = '0';
m_uppercase_letters_to_phone_number_table['1'] = '1';
m_uppercase_letters_to_phone_number_table['A'] = '2';
m_uppercase_letters_to_phone_number_table['B'] = '2';
m_uppercase_letters_to_phone_number_table['C'] = '2';
m_uppercase_letters_to_phone_number_table['2'] = '2';
m_uppercase_letters_to_phone_number_table['D'] = '3';
m_uppercase_letters_to_phone_number_table['E'] = '3';
m_uppercase_letters_to_phone_number_table['F'] = '3';
m_uppercase_letters_to_phone_number_table['3'] = '3';
m_uppercase_letters_to_phone_number_table['G'] = '4';
m_uppercase_letters_to_phone_number_table['H'] = '4';
m_uppercase_letters_to_phone_number_table['I'] = '4';
m_uppercase_letters_to_phone_number_table['4'] = '4';
m_uppercase_letters_to_phone_number_table['J'] = '5';
m_uppercase_letters_to_phone_number_table['K'] = '5';
m_uppercase_letters_to_phone_number_table['L'] = '5';
m_uppercase_letters_to_phone_number_table['5'] = '5';
m_uppercase_letters_to_phone_number_table['M'] = '6';
m_uppercase_letters_to_phone_number_table['N'] = '6';
m_uppercase_letters_to_phone_number_table['O'] = '6';
m_uppercase_letters_to_phone_number_table['6'] = '6';
m_uppercase_letters_to_phone_number_table['P'] = '7';
m_uppercase_letters_to_phone_number_table['R'] = '7';
m_uppercase_letters_to_phone_number_table['S'] = '7';
m_uppercase_letters_to_phone_number_table['7'] = '7';
m_uppercase_letters_to_phone_number_table['T'] = '8';
m_uppercase_letters_to_phone_number_table['U'] = '8';
m_uppercase_letters_to_phone_number_table['V'] = '8';
m_uppercase_letters_to_phone_number_table['8'] = '8';
m_uppercase_letters_to_phone_number_table['W'] = '9';
m_uppercase_letters_to_phone_number_table['X'] = '9';
m_uppercase_letters_to_phone_number_table['Y'] = '9';
m_uppercase_letters_to_phone_number_table['9'] = '9';
}
string DictionaryComparer::NormalizePhoneNumberFormat(string phone_number)
{
const char *phone_number_sink = phone_number.c_str();
int sink_size = phone_number.length();
string result;
map<char, char>::iterator it;
int count(0);
bool flag(false);
for (int i = 0; i < sink_size; i++)
{
if (count == SEPARATE_SYMBOLS_POSITION && !flag)
{
flag = true;
result += "-";
}
it = m_uppercase_letters_to_phone_number_table.find(phone_number_sink[i]);
if (it != m_uppercase_letters_to_phone_number_table.end())
{
count++;
result += m_uppercase_letters_to_phone_number_table.at(phone_number_sink[i]);
}
}
return result;
}
bool DictionaryComparer::IsVaildPhoneNumberFormat(std::string phone_number)
{
if (phone_number.length() != PHONE_NUMBER_LENGTH)
return false;
const char *phone_number_sink = phone_number.c_str();
int sink_size = phone_number.length();
if (phone_number_sink[SEPARATE_SYMBOLS_POSITION] != '-')
return false;
for (int i = 0; i < PHONE_NUMBER_LENGTH; i++)
{
if (i == SEPARATE_SYMBOLS_POSITION)
continue;
if (phone_number_sink[i] == '0' || phone_number_sink[i] == '1'
|| phone_number_sink[i] == '2' || phone_number_sink[i] == '3'
|| phone_number_sink[i] == '4' || phone_number_sink[i] == '5'
|| phone_number_sink[i] == '6' || phone_number_sink[i] == '7'
|| phone_number_sink[i] == '8' || phone_number_sink[i] == '9'
)
{
continue;
}
else
{
return false;
}
}
return true;
}
void DictionaryComparer::AddDictionary(std::string phone_number)
{
if (IsVaildPhoneNumberFormat(phone_number))
{
if (m_dictionary[phone_number])
{
m_dictionary[phone_number]++;
}
else
{
m_dictionary[phone_number] = 1;
}
}
}
void DictionaryComparer::CompareWithDictionary()
{
for (int i = 0; i < m_input.size(); i++)
{
m_dictionary.clear();
for (int j = 0; j < m_input[i].size(); j++)
{
m_input[i][j] = NormalizePhoneNumberFormat(m_input[i][j]);
AddDictionary(m_input[i][j]);
}
m_output.push_back(m_dictionary);
}
}
template <class T1, class T2>
void T1ToT2(T1& t1, T2& t2)
{
std::stringstream ss;
ss << t1;
ss >> t2;
}
void Input(std::vector<std::vector<std::string>> &input)
{
std::string sdatasets_number;
std::string sdataset_number;
std::string data;
std::vector<std::string> datas;
int idatasets_number;
int idataset_number;
std::getline(std::cin, sdatasets_number);
T1ToT2(sdatasets_number, idatasets_number);
for (int i = 0; i < idatasets_number; i++)
{
std::cin.ignore(1024, '\n');
std::getline(std::cin, sdataset_number);
T1ToT2(sdataset_number, idataset_number);
for (int j = 0; j < idataset_number; j++)
{
std::getline(std::cin, data);
datas.push_back(data);
}
input.push_back(datas);
datas.clear();
}
}
void Output(std::vector<std::map<std::string, int>> output)
{
bool flag(false);
for (int i = 0; i < output.size(); i++)
{
if (i != 0)
std::cout << std::endl;
for (std::map<std::string, int>::iterator it = output[i].begin(); it != output[i].end(); it++)
{
if (it->second > 1)
{
std::cout << it->first << " " << it->second << std::endl;
flag = true;
}
}
if (!flag)
{
std::cout << "No duplicates." << std::endl;
}
flag = false;
}
}
int main()
{
DictionaryComparer test;
Input(test.m_input);
test.CompareWithDictionary();
Output(test.m_output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment