MAP学习--POJ1002

本文介绍了一种将包含特殊字符的电话号码字符串转换为统一格式的方法,并利用两种不同的数据结构——vector和map来实现对重复号码的统计。通过对比两种方案的优缺点,最终采用map实现了高效的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10017
题目大意:A-Y(除了Q),每三个字母对应一个数字(从2开始)

A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
输入一串字符(里面含有‘-’,非负整形数字,大写字母),将其转换为指定格式(xxx-xxxx,其中x为数字),相同的输出合并,并且在后面输出个数,个数为1的不输出,如果个数都为1,则输出”No duplicates.”

一开始的想法是使用vector跟string。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;


//将字符串转换为指定格式
string Pair(string str){
    string newStr;
    int j = 0;
    //O(n)
    for (int i = 0; i < str.size(); i++){
        switch (str[i]){
        case '-':break;
        case '1':case '2':case '3':case'4': case'5':case'6':case'7':case'8':case'9':case'0':newStr += str[i]; j++; break;
        case'A':case'B':case'C':newStr += '2'; j++; break;
        case'D':case'E':case'F':newStr += '3'; j++; break;
        case'G':case'H':case'I':newStr += '4'; j++; break;
        case'J':case'K':case'L':newStr += '5'; j++; break;
        case'M':case'N':case'O':newStr += '6'; j++; break;
        case'P':case'R':case'S':newStr += '7'; j++; break;
        case'T':case'U':case'V':newStr += '8'; j++; break;
        case'W':case'X':case'Y':newStr += '9'; j++; break;
        default:break;
        }
        if (j == 3){
            newStr += '-';
            j++;
        }
    }
    return newStr;
}


//结构,用来排序
struct StrAndNum{
public:
    string str;
    int count;
    StrAndNum(string newStr, int num){
        str = newStr;
        count = num;
    }
};

bool cmp(const StrAndNum& san1,const StrAndNum& san2){
    return san1.str < san2.str;
}

int main(){
    vector<string> dir;//用来接收输入的vector
    vector<StrAndNum>result;//用来存放结果的vector
    int cnt;
    cin >> cnt;
    int temp = cnt;

    //输入电话号码
    //O(n)
    while (cnt--){
        string number;
        cin >> number;
        dir.push_back(number);
    }

    bool flag1 = 0; //用来判断是否个数全都为1
    //转换电话号码同时插入vector中
    //O(n^2)
    for (int i = 0; i < dir.size(); i++){
        bool flag = 1;  //用来判断是否存在,如果存在则不插入
        //将vector第I个元素换成string
        string str = Pair(dir.at(i));

        //插入
        ////vector空则插入
        if (result.empty()){
            StrAndNum san(str, 1);
            result.push_back(san);
            continue;
        }
        ////vector不空,则需要遍历查找有没有相同元素
        for (int j = 0; j < result.size(); j++){
            if (str == result[j].str){
                result[j].count++;
                flag1 = 1;
                flag = 0;
                break;
            }
        }
        if (flag){
            StrAndNum san(str, 1);
            result.push_back(san);
        }
    }

    //O(logn)
    int size = result.size();
    sort(result.begin(), result.end(), cmp);

    //O(n)
    if (flag1)
    for (int i = 0; i < result.size(); i++){
        if (result[i].count 1)
            //cout << result[i].str << ' ' << "No duplicates." << endl;
            continue;
        else{
            cout <<`
result[i].str << ' ' << result[i].count << endl;
        }
    }
    else
        cout << "No duplicates." << endl;
}

上面的代码进行了多次的遍历,需要耗费相当多的时间,结果TLE了。
下面使用STL另外的容器Map,特点是每个元素都有一个key和value,然后例如

map<foo1,foo2>m1; 
m1[foo1]=foo2;

即我们可以将key设为电话号码,value设为个数,每次输入一个电话号码后的操作就是

++dir[number];

这样如果该号码没有出现过则value加一,并自动按照key的顺序排列。
代码如下:

#include<iostream>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;


//因为int最多可以存10位,而电话只有7位,使用int排序更快
map<int, int> dir;
//通过数组快速的将字母转化为为对应的数字
int match[26] = { 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7,7, 8, 8, 8, 9, 9, 9,9};

int main(){
    char al = 'A';
    dir.clear();
    char ch;
    int cnt = 0;
    cin >> cnt;
    getchar();
    while (cnt--){
        int number=0, j = 6;
        while ((ch=getchar())!='\n'){
            if (ch == '-')continue;
            if (ch <= '9'&&ch >= '0'&&j>=0){
                number += (ch-'0')*pow(10.0, j--);
            }
            if (ch <= 'Y'&&ch >= 'A'&&j>=0){
                number += (match[ch - 'A'])*pow(10.0, j--);
            }
        }
        ++dir[number];
    }

    map<int, int>::iterator iter = dir.begin();
    bool flag = true;
    while (iter != dir.end()){
        if (iter->second > 1){ //map的迭代器有两个值,first是key出使用printf可以控制数字的个数,如果使用cout则需判断

//使用printf确保输出的数字的位数,如果使用cout,则要判断除以10000后是否大//于100,10,1比较麻烦
            printf("%03d-%04d %d\n", (iter->first) / 10000, (iter->first) % 10000, iter->second);
        }
        iter++;
    }
    if (flag)
        cout << "No duplicates." << endl;
}

Map的其他使用
1.map::insert
仍然用上面的例子

dir.insert(make_pair(num,1));

insert的实参是pair对象,make_pair则返回pair。
2.map::count
使用count检查map对象中某key是否存在,返回值只能是0或1
3.map::find
find返回指向元素的迭代器,如果元素不存在返回end迭代器。

int occurs=0map<intint>::iterator it=dir.find(1234567);
if(it!=dir.end())
occurs=it->second;

4.map::erase
返回被删除的元素个数,1或0(元素不存在);

dir.erase(1234567);//删除键值为1234567的元素
dir.erase(dir.begin(),dir.end());//从dir的中删除一段范围内的元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值