PKU ACM 1002 487-3279 C++实现

来源:互联网 发布:石柱农村淘宝招聘 编辑:程序博客网 时间:2024/05/17 03:51
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>


using namespace std;


class LiQiang
{
private:
inline char convert(char ch)
{
if (ch <= '9' && ch >= '0')
{
return ch;
}
if ('A' == ch || 'B' == ch || 'C' == ch)
return '2';
if ('D' == ch || 'E' == ch || 'F' == ch)
return '3';
if ('G' == ch || 'H' == ch || 'I' == ch)
return '4';
if ('J' == ch || 'K' == ch || 'L' == ch)
return '5';
if ('M' == ch || 'N' == ch || 'O' == ch)
return '6';
if ('P' == ch || 'R' == ch || 'S' == ch)
return '7';
if ('T' == ch || 'U' == ch || 'V' == ch)
return '8';
if ('W' == ch || 'X' == ch || 'Y' == ch)
return '9';
return '-';
}


static int cmp(pair<string, int> & x, pair<string, int> & y)
{
return x.second < y.second;
}


map<string, int> m_map;
vector<pair<string, int> > m_vector;


int num;


public:
LiQiang()
{
num = 0;
}


void add(string str)
{
num++;


string::iterator itor_str = str.begin();


for (; itor_str != str.end(); itor_str++)
{
while ( itor_str != str.end() && '-' == convert(*itor_str))
{
itor_str = str.erase(itor_str);
}
if (itor_str != str.end())
{
*itor_str = convert(*itor_str);
}
else
{
break;
}
}
str.insert(3, 1, '-');


map<string, int>::iterator itor_map;
itor_map = m_map.find(str);
if (m_map.end() == itor_map)
{
m_map[str] = 1;
} else
{
itor_map->second++;
}
}


void qsort()
{
map<string, int>::iterator itor_map = m_map.begin();


for (; itor_map!=m_map.end(); itor_map++)
{
m_vector.push_back(make_pair(itor_map->first, itor_map->second));
}
sort(m_vector.begin(), m_vector.end(), cmp);
}


friend ostream & operator << (ostream & os, LiQiang &tmp);
};


ostream & operator << (ostream & os, LiQiang &tmp)
{
if (tmp.num == tmp.m_map.size())
{
os << "No duplicates." << endl;
}
else
{
map<string, int>::iterator itor = tmp.m_map.begin();
for (; itor != tmp.m_map.end(); itor++)
{
if (1 == itor->second)
{
continue;
}
else
{
os << itor->first << " " << itor->second << endl;
}
}
}
return os;
}
int main()
{
string str;
char tmp[100];
int num;
LiQiang res;


scanf("%d", &num);
for (int i = 0; i < num; i++)
{
scanf("%s", tmp);
str = tmp;
res.add(str);
}


cout << res;
system("PAUSE");
return 0;
}
0 0
原创粉丝点击