这道题目标记是个简单题,但我却写了很久,也很长时间没有写题了。写题也是个熟练活。额外写了两个函数,一个是字符串是否和模式匹配的函数(这个也是根据题目的特定限制要求做的),一个是检查某一个呼入号码是否在白名单里的函数。这里需要选用一个顺序型的容器,以满足题目中的按照给定记录中号码首次呼入出现的先后顺序这一要求。 选用了vector,还用了结构体的类型以存储呼入的号码+接通次数+拒接次数。逻辑控制基本采用的都是 if else,写的很长,感觉是很笨的写法,很多值得优化的地方。不知道有没有更为简洁的写法
/*
* Copyright (c) Huawei Technologies Co., Ltd. All rights reserved.
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
*/
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;
struct pNode{
string telNum;
int connectTimes;
int refuseTimes;
pNode(string x):telNum(x), connectTimes(0),refuseTimes(0) {};
};
typedef pair<int,int> P;
class Solution {
public:
//检查两个字符串是否匹配
bool match(string s, string pattern){//号码仅为数字,长度范围3到11位
int i=0, j=0;//其实最好用i和j
bool flag=true;//标记两个串是否匹配
size_t s_size=s.size(), pattern_size=pattern.size();
while(flag==true && i<s_size && j<pattern_size ){
if(s_size<pattern_size) return false;
if(s[i]==pattern[j]){
i++;
j++;
}
else if(s[i]!=pattern[j]) {
if(pattern[j]=='*'){
return true;
}
else{//pattern[p2]不是*的话
flag=false;
}
}
}
return flag;
}
//检查是否在白名单里
bool IsWhite(string s, set<string> whiteList){
if(whiteList.count(s)==1){
return true;
}
else{
bool flag=false;
auto it=whiteList.begin();
while(it!=whiteList.end() && flag==false){
string pattern=*it;
if(match(s,pattern)){
return true;
}
else{
it++;
}
}
return flag;
}
}
// 待实现函数,在此函数中填入答题代码;
vector<string> GetPhoneRecord(const vector<pair<char, string>> &records)
{
vector<string> result;
set<string> whiteList;//白名单
set<string> callinList;//呼入的名单存放
//按照号码首次呼入的先后顺序
//pNode* pHead=nullptr;
vector<pNode> v;
for(auto it=records.begin();it!=records.end();++it){
string phoneNum=it->second;
cout<<"phoneNum:"<<phoneNum<<endl;
if((it->first)=='C'){//如果是一个呼入的话,先查是不是一个之前有过的呼入(在呼入名单里查)
cout<<"This is a call"<<endl;
if(IsWhite(phoneNum,whiteList)){//如果在白名单里的话,再看之前有没有打进来过
if(callinList.count(phoneNum)==1){//如果之前有打进来过,那么接通次数++
for(int i=0;i<v.size();++i){
if(v[i].telNum==phoneNum){
v[i].connectTimes++;
break;
}
}
}
else {//在白名单里并且之前没有打来过
pNode temp(phoneNum);
temp.connectTimes++;
v.push_back(temp);
callinList.insert(phoneNum);
}
}
else{//如果不在白名单里的话,还是要先看之前有没有打来过
if(callinList.count(phoneNum)==1){//不在白名单,并且之前打来过,拒接次数加加
for(int i=0;i<v.size();++i){
if(v[i].telNum==phoneNum){
v[i].refuseTimes++;
break;
}
}
}
else{//之前没有打来过
pNode temp(phoneNum);
temp.refuseTimes++;
v.push_back(temp);
callinList.insert(phoneNum);
}
}
callinList.insert(phoneNum);
}
else if(it->first=='W'){//如果是一个新加白名单的话
whiteList.insert(phoneNum);
}
}
cout<<"vector v:"<<endl;
for(auto it=v.begin();it!=v.end();++it){
cout<<(*it).telNum<<" "<<(*it).connectTimes<<" "<<(*it).refuseTimes<<endl;
string temp=(*it).telNum+" "+to_string((*it).connectTimes)+" "+to_string((*it).refuseTimes);
result.push_back(temp);
}
cout<<"whitelist:"<<endl;
for(auto it=whiteList.begin();it!=whiteList.end();++it){
cout<<*it<<endl;
}
cout<<endl;
return result;
}
};
// 以下为考题输入输出框架,此部分代码不建议改动
inline int ReadInt()
{
int number;
std::cin >> number;
return number;
}
template<typename T>
inline void WriteVector(const vector<T>& objects, char delimeter = ' ')
{
auto it = objects.begin();
if (it == objects.end()) {
return;
}
cout << *it;
for (++it; it != objects.end(); ++it) {
cout << delimeter << *it;
}
cout << endl;
}
int main()
{
int num = ReadInt();
vector<pair<char, string>> records;
for (int loop = 0; loop < num; loop++) {
char op;
string phone;
cin >> op >> phone;
records.push_back(make_pair(op, phone));
}
Solution solu;
vector<string> result = solu.GetPhoneRecord(records);
WriteVector(result, '\n');
return 0;
}
补充:之前碰到这种输入不会写,这下可以记住这种输入方式,使用pair
make_pair(a, b);
// 以a和b的值创建一个新的pair对象(临时的对象),其元素类型分别是a和b的类型。