您的位置:首页 > 编程语言 > C语言/C++

PKU ACM 1002 487-3279 C++实现

2014-06-12 11:15 489 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm c++