先欣赏一个美图吧
给一个序列
C 13300000000 代表 C 呼入电话 ,电话号码 13300000000
W 037128* 代表 W 白名单 ,后面可以模糊匹配,而且保证出现在最后一个
如果呼入时电话在白名单中,则可以呼入,否则拒绝
请按输入顺序 输出 每个电话号的 呼入次数,拒绝次数 例如 13300000000 1 0 表示 该号码可以呼入1次,被拒绝0次
样例:
输入:
9
C 13300000000
W 13144444444
C 13144444444
W 037128*
C 03712832233
C 03712832333
C 03712832333
W 03712832*
C 03712832444
输出:
13300000000 0 1
13144444444 1 0
03712832233 1 0
03712832333 2 0
03712832444 1 0
c++
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include <set>
using namespace std;vector<string> Report(const vector<pair<char, string>> &records){vector<string> result;if(records.empty()) {return result;}unordered_map<int, set<string>> mp;unordered_map<string, pair<int, int>> res;vector<string> order;for (auto record : records) {if (record.first == 'W') {mp[record.second.size()-1].insert(record.second.substr(0, record.second.size()-1));} else if(record.first == 'C') {if(res.find(record.second) == res.end()) {order.push_back(record.second);res[record.second].first = 0;res[record.second].second = 0;}bool flag = true;for(auto m : mp){if(m.second.find(record.second.substr(0, m.first)) != m.second.end()){res[record.second].first++;flag = false;break;}}if(flag) {res[record.second].second++;}}}for(auto r : order) {string s = r + " " + to_string(res[r].first) + " " + to_string(res[r].second);result.push_back(s);}return result;}
// 处理输入
int main(){
vector<string> res;int num;pair<char, string> p1;vector<pair<char, string>> records;cin >> num;for (size_t i = 0; i < num; i++){cin >> p1.first >> p1.second;records.push_back(p1);}//for (size_t i = 0; i < records.size(); i++)//{// cout << records[i].first << records[i].second << endl;//}res = Report(records);//for (size_t i = 0; i < res.size(); i++)//{// cout << i << res[i] << endl;//}return 0;}
python
from typing import List, Tuple
from collections import defaultdictdef Report(records: List[Tuple[str, str]]) -> List[str]:if not records:return []mp = defaultdict(set)res = {}order = []for record in records:if record[0] == 'W':mp[len(record[1])-1].add(record[1][:-1])elif record[0] == 'C':if record[1] not in res:order.append(record[1])res[record[1]] = [0, 0]flag = Truefor m in mp:if record[1][:m] in mp[m]:res[record[1]][0] += 1flag = Falsebreakif flag:res[record[1]][1] += 1result = []for r in order:s = r + " " + str(res[r][0]) + " " + str(res[r][1])result.append(s)return resultnum = int(input())
records = []
for i in range(num):p1 = input().split()records.append((p1[0], p1[1]))for record in records:print(record[0] + record[1])res = Report(records)for i, r in enumerate(res):print(str(i) + r)