目录

题目

思路

Code


题目

某个产品的 restful API 集合部署在服务器集群的多个节点上,近期对客户端访问日志进行了采集,需要统计各个API的访问频次,根据热点信息在服务器节点之间做负载均衡,现在需要实现热点信息统计查询功能。
restful API 是由多个层级构成,层级之间使用/连接,如 A/B/C/D 这个地址,A属于第一级,B属于第二级,C属于第三级,D属于第四级。
现在负载均衡模块需要知道给定层级上某个名字出现的频次,未出现过用0表示,实现这个功能。

输入描述
第一行为N,表示访问历史日志的条数,0 < N<= 100.
接下来N行,每一行为一个restful API的URL地址,约束地址中仅包含英文字母和连接符,最大层级为10,每层级字符串最大长度为10。
最后一行为层级L和要查询的关键字
输出描述
输出给定层级上,关键字出现的频次,使用完全匹配方式 (大小写敏感)


示例1:

输入:

5
/huawei/computing/no/one
/huawei/computing
/huawei
/huawei/cloud/no/one
/huawei/wireless/no/one
2 computing
输出
2
说明
在第二层级上,computing出现了2次,因此输出2

示例2:

输入:

输入
5
/huawei/computing/no/one
/huawei/computing

/huawei
/huawei/cloud/no/one
/huawei/wireless/no/one
4 two
输出

0
说明
存在第四层级的URL上,没有出现two,因此频次是0

思路

1:典型的长题干类型的简单题。

2:第一步先把每个输入字符串按照分隔符分割,统计每个层级上各个节点的出现频次,保存于hashmap

3:最后对应找一下输出即可。

编辑

Code

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import functools
import sys
from collections import Counter, defaultdict
import copy
from itertools import permutations
import re
import math
import sys
from queue import Queue
 
count = int(input())

#每个层级上对应字符串的出现次数
node_info = {}

i=0
while(True):
    if(i>=count):
        break
    else :
        inputs = input().split("/")
        #细节要注意从1开始
        for j in range(1, len(inputs)):
            if(j in node_info):
                if(inputs[j] in node_info[j]):
                    node_info[j][inputs[j]] +=1
                else :
                    node_info[j][inputs[j]] =1
                
            else :
                node_info[j] = {}
                node_info[j][inputs[j]] =1
    i+=1

params = input().split(" ")
L = int(params[0])
target_str = params[1]

output_count = 0
if(L in node_info) :
    if(target_str in node_info[L]):
        output_count = node_info[L][target_str]
print(output_count)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.