POJ1002-487-3279
2017-09-16 15:24
211 查看
#include<iostream>
#include<map>
#include<string>
using namespace std;
char convert(char character) {
switch (character) {
case 'A':case 'B':case 'C': return '2';
case 'D':case 'E':case 'F': return '3';
case 'G':case 'H':case 'I': return '4';
case 'J':case 'K':case 'L': return '5';
case 'M':case 'N':case 'O': return '6';
case 'P':case 'R':case 'S': return '7';
case 'T':case 'U':case 'V': return '8';
case 'W':case 'X':case 'Y': return '9';
}
}
int main()
{
int N;
cin >> N;
map<string, int> count_telep;
for (int i = 0; i < N; i++) {
string input, tele;
cin >> input;
for (unsigned int j = 0; j < input.length(); j++) {
if (tele.length() == 3) tele += '-';
if (input[j] >= '0'&&input[j] <= '9')
tele += input[j];
if (input[j] >= 'A'&&input[j] <= 'Z')
tele += convert(input[j]);
}
if (count_telep.count(tele) == 0)
count_telep[tele] = 0;
count_telep[tele] += 1;
}
map<string, int>::iterator iter;
bool flag = false;
for (iter = count_telep.begin(); iter != count_telep.end(); iter++) {
if (iter->second > 1) {
cout << iter->first << ' ' << iter->second<< endl;
flag = true;
}
}
if (!flag)
cout << "No duplicates. ";
return 0;
}
map为方便查找,按一定顺序存储,key为string类型时按照字典排序。
遍历map的两种方式:
定义为map<int,string>的元素遍历:
#include<map>
#include<string>
using namespace std;
char convert(char character) {
switch (character) {
case 'A':case 'B':case 'C': return '2';
case 'D':case 'E':case 'F': return '3';
case 'G':case 'H':case 'I': return '4';
case 'J':case 'K':case 'L': return '5';
case 'M':case 'N':case 'O': return '6';
case 'P':case 'R':case 'S': return '7';
case 'T':case 'U':case 'V': return '8';
case 'W':case 'X':case 'Y': return '9';
}
}
int main()
{
int N;
cin >> N;
map<string, int> count_telep;
for (int i = 0; i < N; i++) {
string input, tele;
cin >> input;
for (unsigned int j = 0; j < input.length(); j++) {
if (tele.length() == 3) tele += '-';
if (input[j] >= '0'&&input[j] <= '9')
tele += input[j];
if (input[j] >= 'A'&&input[j] <= 'Z')
tele += convert(input[j]);
}
if (count_telep.count(tele) == 0)
count_telep[tele] = 0;
count_telep[tele] += 1;
}
map<string, int>::iterator iter;
bool flag = false;
for (iter = count_telep.begin(); iter != count_telep.end(); iter++) {
if (iter->second > 1) {
cout << iter->first << ' ' << iter->second<< endl;
flag = true;
}
}
if (!flag)
cout << "No duplicates. ";
return 0;
}
map为方便查找,按一定顺序存储,key为string类型时按照字典排序。
遍历map的两种方式:
定义为map<int,string>的元素遍历:
map<int,string> maps for(int i=0;i<maps.size();i++){ string=maps[i]; }定义为map<string,int>的元素遍历:
map<string,int> maps; map<string,int>::iterator iter; for(iter=maps.begin();iter!=maps.end();iter++){ string=iter->first; int=iter->second; }
相关文章推荐
- poj1002 487-3279
- POJ1002 487-3279
- POJ1002_487-3279(快速排序)
- POJ1002-487-3279 map应用
- poj1002-487-3279(字符串处理)
- poj1002 487-3279
- POJ1002-487-3279
- POJ1002-487-3279
- POJ1002--487-3279 翻译
- poj1002 487-3279(电话号码重复次数)
- POJ1002--487-3279 翻译
- POJ1002 487-3279【查表】(Bailian1002, Bailian2974)
- poj1002 487-3279 (简单字符串处理)
- 快速排序 487-3279 poj1002
- POJ1002 487-3279 直接哈希模拟
- poj1002“487-3279”解题报告
- POJ1002 487-3279
- POJ 2974 487-3279 解题报告
- poj 1002 "487-3279"
- POJ 1002求487-3279解题报告——【PKU ACM】