Every email consists of a local name and a domain name, separated by the @ sign.
For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.
Besides lowercase letters, these emails may contain '.'s or '+'s.
If you add periods (’.’) between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.)
If you add a plus (’+’) in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)
It is possible to use both of these rules at the same time.
Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?
Example 1:
Input: [“test.email+alex@leetcode.com”,“test.e.mail+bob.cathy@leetcode.com”,“testemail+david@lee.tcode.com”]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
Note:
1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one ‘@’ character.
本来是个水题,结果因为我吧erase用错了,调试了一会才解决问题。
erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
注意第2,3种,参数均是迭代器,不是int类型的。
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
string changeEmail(string str){
string samestr;
for(int i=0;i<str.length();i++){
if(str[i]=='.'){
str.erase(i--,1);
}
else if(str[i]=='+'){
str.erase(i--,str.find('@')-i); //erase的参数
break;
}else if(str[i]=='@'){
break;
}
}
//cout<<str<<endl;
return str;
}
int numUniqueEmails(vector<string>& emails) {
set<string> uniemail;
for(int i=0;i<emails.size();i++){
uniemail.insert(changeEmail(emails[i]));
}
return uniemail.size();
}
int main()
{
cout << "Hello world!" << endl;
string str[]={"c.o.t.m.f.q.+rt.i.l@zuvn.skdsq.com","d.c.fw.jc.c.gt+w@yzuq.com","n.kvtb+a.iq.c.l@rupfxkfr.com","xckqzemg+f.h@hbtp.com",
"mh+f.o+k.d.e.x.fe@gywxo.n.com","d.c.fw.jc.c.gt+q@yzuq.com","hh.a.fm.jea.s+t@epo.com","xckqzemg+dx@hbtp.com","n+gz.fyk+f.oi+e@j.fe.com",
"vqvp.p.lvt.t+k@rexcs.com","nnwhgqn+qm.n@chmgo.com","mrjrugbrh+e@z.zvnnhswsq.com","mrjrugbrh+s@z.zvnnhswsq.com","mh+fclendxh@gywxo.n.com",
"t.bgivgn.yh+l@sytfq.mgr.com","cv.oau.d.zsub+x@ybzkk.com","oww.sotdsb.j+w@aadoc.r.com","y.qdv+v+b+bk.di@wd.lvz.com","nnwhgqn+h.se@chmgo.com",
"q.y+un.d.d.q+n+or@rq.wxd.com"};
int len = sizeof(str)/sizeof(str[0]);
vector<string> emails(str,str+len);
cout<<numUniqueEmails(emails)<<endl;
return 0;
}