OJ[号码拦截]

题目描述:
终端铲=产品的儿童电话手表具有防骚扰特性,对电话呼入按如下规则处理:
如果来电号码在白名单中,则电话要接通。否则电话自动拒接。

先给定一组电话呼入和白名单的操作记录,格式为:
C 13300000000 表示一条电话呼入记录。
W 13144444444 表示添加一个号码到白名单中;
W 03712832* 表示以03712832开头的号码都为白名单(*表示通配符,仅在结尾)。
请按照给定的过程记录,分别统计每个呼入号码的接通和拒接次数;
每个号码的统计记录的格式如: 13144444444 1 3,以单个空格隔开,表示号码13144444444 接通1次,拒接3次。
统计记录的输出顺序;按照给定记录中号码的首次呼入出现的先后顺序。

解题要求:
时间限制:1000ms, 内存限制:256M
输入:
第一行正数n,表示共有n条电话呼入和白名单操作记录,取值范围为[1,3000].
随后n行时电话呼入和白名单操作记录。
注:用例保证,号码仅为数字,长度范围为[3,11]
输出:
每一行一个字符串,代表一个呼入号码的统计记录

样例:
输入样例1:
7
C 13300000000
W 13144444444
C 13144444444
C 03712832444
C 03712832233
W 03712832*
C 03712832444

输出样例1:
13300000000 0 1
13144444444 1 0
03712832444 1 1
03712832233 0 1
提示样例1
13300000000 呼入了1次,不在白名单中,接通0次,拒接1次。
13144444444 呼入前已加入报名单,接通一次,拒接0次
白名单03712832*加入前,03712832233和03712832444都被拒接了,加入后03712832444接通一次。
注意:需要按号码出现的顺序输入统计记录。

C++解答:

解题思路:

1、定义一个string类型的队列,保存呼入号码出现的顺序,出现过的只保留1次

      定义一个数组存放白名单号码

      定义一个unordered_map<string, pair<int, int>>,存放结果,最后需要转为字符串

2、判断是否在白名单内,在接通+1,不在拒接+1

3、W操作,则加入白名单列表中

代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;

class Solution {
public:
    vector<string> getPhoneBlockTimes(vector<pair<char, string>> phoneRecords)
    {
        int i = 0;
        for(const auto &[opt, phoneNum] : phoneRecords) {
            if(opt == 'C') {
                if(ans.count(phoneNum) == 0) {
                    phoneFifo.push(phoneNum);
                }
                if(isInWhiteList(phoneNum)) {
                    ans[phoneNum].first ++;
                } else {
                    ans[phoneNum].second ++;
                }
            } else if(opt == 'W') {
                whiteList.push_back(phoneNum);
            }
        }
        i = 0;
        vector<string> result;
        while(!phoneFifo.empty()) {
            string s = phoneFifo.front();
            phoneFifo.pop();
            result.push_back(s + " " + to_string(ans[s].first) + " " +to_string(ans[s].second));
        }
        return result;
    }


private:
    queue<string> phoneFifo;
    vector<string> whiteList;
    unordered_map<string, pair<int, int>> ans;
    bool isInWhiteList(const string phoneNum) {
        string s = phoneNum;
        for(auto s1 : whiteList) {
            int pos = 0;
            pos = s1.find('*');
            if(pos != 0) {
                if(s1.substr(0, pos) == s.substr(0, pos)) {
                    return true;
                } else {
                    continue;
                }

            } else {
                if(s == s1) {
                    return true;
                } else {
                    continue;
                }
            }
        }
        return false;
    }

};
int main()
{
    int count;
    cin >> count;
    vector<pair<char, string>> phoneRecords;
    for(int i = 0; i < count; i++) {
        char oper;
        string phoneNum;
        cin >> oper >> phoneNum;
        phoneRecords.push_back(make_pair(oper, phoneNum));
    }

#if 0
    for(auto it : phoneRecords) {
        cout << it.first << " " << it.second << endl;;
    }
#endif
    Solution solu;
    vector<string> result = solu.getPhoneBlockTimes(phoneRecords);

    for(auto s : result) {
        cout << s << endl;
    }
    return 0;
}

python解答:

思路: 利用dict的defaultdict方法,创建一个可以赋值的dict

ans = defaultdict(lambda : [0, 0])   (该操作需要引入from collections import defaultdict)

具体代码如下:

#!/usr/bin/env python
# coding=utf-8
from collections import defaultdict
class Solution:
    def getPhoneRecords(self, phoneRecords):
        set1 = set()
        set2 = set()
        ans = defaultdict(lambda : [0, 0])
        for phoneRecord in phoneRecords:
            if phoneRecord[0] == 'W':
                if phoneRecord[1][-1] == '*':
                    set2.add(phoneRecord[1][:-1])
                else:
                    set1.add(phoneRecord[1])
            else:
                if phoneRecord[1] in set1 or any(phoneRecord[1].startswith(x) for x in set2):
                    ans[phoneRecord[1]][0] += 1
                else:
                    ans[phoneRecord[1]][1] += 1

        return ['{0} {1} {2}'.format(k, v[0], v[1]) for k, v in ans.items()]

if __name__ == "__main__":
    count = int(input().strip())
    phoneRecords = [input().strip().split() for _ in range(count)]
    function = Solution()
    result = function.getPhoneRecords(phoneRecords);
    for s in result :
        print(s)

any函数,用于给定的可迭代参数是否全部为False,则返回False,如果一个为True,则返回True

startswith(x) 用于检查已给字符串是否以指定的前缀开头。

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
北京大学oj题目,已提交AC。原题目如下: Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10. The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: 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 There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. Input The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. Output Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: No duplicates. Sample Input 12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 Sample Output 310-1010 2 487-3279 4 888-4567 3

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值