Message Flood——map初级应用

博客探讨了map在处理字符串哈希问题中的初级应用,特别是在统计未回复消息数量的场景下。文章强调了两个关键点:一是map数组初始化的重要性,二是处理字母串时要忽略大小写差异。通过错误示例和正确代码展示了解决方案。

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

Think:
1知识点:map初级应用
2反思:map数组不要忘记初始化
3题意概述:n个朋友名单,m个已回消息名单,问还需回复的朋友数量
4题意注意:
1>字母串中大小写不敏感,需转化
2>map数组应用时不要忘记初始化

SDUT题目链接

以下为Wrong Answer代码——map数组没有初始化

#include <bits/stdc++.h>

using namespace std;

void tra(char *st);

char st[14];

int main(){
    map<string, int> m1;
    int n, m;
    while(scanf("%d", &n) && n){
        scanf("%d", &m);
        getchar();
        for(int i = 0; i < n; i++){
            scanf("%s", st);
            tra(st);
            m1[st]++;
        }
        for(int i = 0; i < m; i++){
            scanf("%s", st);
            tra(st);
            if(m1.count(st)){
                m1.erase(st);
            }
        }
        int len = m1.size();
        printf("%d\n", len);
    }
    return 0;
}
void tra(char *st){
    int len = strlen(st);
    for(int i = 0; i < len; i++){
        if(st[i] >= 'A' && st[i] <= 'Z'){
            st[i] = st[i] - 'A' + 'a';
        }
    }
}


/***************************************************
User name: 
Result: Wrong Answer
Take time: 212ms
Take Memory: 8540KB
Submit time: 2017-07-14 08:13:07
****************************************************/

以下为Wrong Answer代码——字母串中大小写不敏感需转化

#include <bits/stdc++.h>

using namespace std;

void tra(char *st);

char st[14];

int main(){
    map<string, int> m1;
    int n, m;
    while(scanf("%d", &n) && n){
        scanf("%d", &m);
        getchar();
        m1.clear();///初始化—清空所有元素
        for(int i = 0; i < n; i++){
            scanf("%s", st);
            /*tra(st);*/
            m1[st]++;
        }
        for(int i = 0; i < m; i++){
            scanf("%s", st);
            /*tra(st);*/
            if(m1.count(st)){
                m1.erase(st);
            }
        }
        int len = m1.size();
        printf("%d\n", len);
    }
    return 0;
}
void tra(char *st){
    int len = strlen(st);
    for(int i = 0; i < len; i++){
        if(st[i] >= 'A' && st[i] <= 'Z'){
            st[i] = st[i] - 'A' + 'a';
        }
    }
}


/***************************************************
User name: 
Result: Wrong Answer
Take time: 140ms
Take Memory: 2344KB
Submit time: 2017-07-14 08:21:44
****************************************************/

以下为Accepted代码

#include <bits/stdc++.h>

using namespace std;

void tra(char *st);

char st[14];

int main(){
    map<string, int> m1;
    int n, m;
    while(scanf("%d", &n) && n){
        scanf("%d", &m);
        getchar();
        m1.clear();///初始化—清空所有元素
        for(int i = 0; i < n; i++){
            scanf("%s", st);
            tra(st);
            m1[st]++;
        }
        for(int i = 0; i < m; i++){
            scanf("%s", st);
            tra(st);
            if(m1.count(st)){
                m1.erase(st);
            }
        }
        int len = m1.size();
        printf("%d\n", len);
    }
    return 0;
}
void tra(char *st){
    int len = strlen(st);
    for(int i = 0; i < len; i++){
        if(st[i] >= 'A' && st[i] <= 'Z'){
            st[i] = st[i] - 'A' + 'a';
        }
    }
}


/***************************************************
User name: 
Result: Accepted
Take time: 160ms
Take Memory: 2340KB
Submit time: 2017-07-14 08:16:30
****************************************************/

以下为Accepted代码

#include <bits/stdc++.h>

using namespace std;

void tra(char *st);

char st[14];

int main(){
    map<string, int> m1;
    int n, m;
    while(scanf("%d", &n) && n){
        scanf("%d", &m);
        getchar();
        m1.clear();///初始化—清空所有元素
        for(int i = 0; i < n; i++){
            scanf("%s", st);
            tra(st);
            m1[st]++;
        }
        for(int i = 0; i < m; i++){
            scanf("%s", st);
            tra(st);
            if(m1.count(st)){
                m1.erase(st);
            }
        }
        int len = m1.size();
        printf("%d\n", len);
    }
    return 0;
}
void tra(char *st){
    int len = strlen(st);
    for(int i = 0; i < len; i++){
        st[i] = towlower(st[i]);/*towlower()将大写字母转换为小写字母*/
    }
}


/***************************************************
User name: 
Result: Accepted
Take time: 164ms
Take Memory: 2340KB
Submit time: 2017-07-14 08:24:22
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值