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;
}
#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;
}
相关文章推荐
- POJ 1002求487-3279解题报告——【PKU ACM】
- Pku acm 1002 487-3279 排序算法解题报告(一) ----二叉查找数(BST)
- 487-3279 - PKU 1002
- 北大 ACM 1002 487-3279
- PKU_1002_487-3279
- ACM-北大1002 487-3279
- PKU 1002 487-3279 问题的解决
- ACM —— 1002 487-3279
- PKU 1002 487-3279
- 487-3279 - PKU 1002
- C++——字符串例题——487-3279(POJ1002)
- 【poj1002】487-3279 题解&代码(c++)
- pku 1002 487-3279
- ACM打怪之旅—1002 487-3279
- POJ1002_487-3279_C++
- [POJ 1002] 487-3279 C++解题报告
- OpenJudge / POJ 1002 487-3279 解题报告 C++
- 北大 ACM 1002 题 487-3279 的 菜鸟JAVA解
- PKU-1002-487-3279
- 【poj1002】487-3279