CodeForces 589A Email Aliases

该博客主要介绍了如何利用STL中的map数据结构来解决CodeForces上的589A题目,即Email Aliases的模拟问题。博主在文章中提到,最初尝试使用结构体,但考虑到可能占用过多空间和操作复杂性,最终选择了map。文章详细阐述了解题思路和代码实现的关键点,包括如何通过map存储和匹配电子邮件别名。

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

模拟题加上STL map的应用。

开始竟然想用结构体来处理,后来觉得不仅会占大量空间,并且不好写……

细节见代码。

输入:

6
ICPC.@bmail.com
p+con+test@BMAIL.COM
P@bmail.com
a@bmail.com.ru
I.cpc@Bmail.Com
a+b@bmail.com.ru

输出:

4
2 ICPC.@bmail.com I.cpc@Bmail.Com
2 p+con+test@BMAIL.COM P@bmail.com
1 a@bmail.com.ru
1 a+b@bmail.com.ru

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A,B)memcpy(A,B,sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fLL;
const double EPS=1e-9;
const double OO=1e20;
const double PI=acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
int gcd (const LL &a,const LL &b) {return b==0?a:gcd (b,a%b);}
using namespace std;
char s[105],t[105];
void Iint (char s[],char S[]) {
    strcpy (S,s); int atp=0;//复制一遍,保留原串
    for (int i=0; s[i]; i++) {
        s[i]=tolower (s[i]);//因为地址不区分大小写
        if (s[i]=='@') { atp=i; }//记录@符号的位置
    }
    if (strcmp (s+atp+1,"bmail.com") ==0) {//只有这个域名才有以下规则
        int pos=0;
        for (int j=0; j<atp; j++) {
            if (s[j]=='.') { continue; }//忽略所有点
            if (s[j]=='+') { break; }//‘+’至‘@’符号中间所有字符忽略
            s[pos++]=s[j];
        }
        for (int i=atp; s[i]; i++) { s[pos++]=s[i]; }//拷贝剩下部分
        s[pos]=0;
    }
}
map<string,int>eadd;
vector<string>ans[20020];
int num[20020];
int main() {
    int n,k=0; scanf ("%d",&n);
    eadd.clear();
    for (int i=1; i<=n; i++) {
        scanf ("%s",s);
        Iint (s,t);
        if (eadd.find (s) ==eadd.end() ) { eadd[s]=++k; }//如果处理后的字符串没出现过,那么就是一个新类
        int o=eadd[s]; ++num[o];//请仔细体会 o 的用处
        ans[o].push_back (t);//存储原串至同类代码存储区
    }
    printf ("%d\n",k);
    for (int i=1; i<=k; i++) {
        printf ("%d",num[i]);//各类别总数
        for (int j=0; j<ans[i].size(); j++) {
            cout<<" "<<ans[i][j];//输出原串
        }
        puts ("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值