机试题目

API集群负载统计

时间限制:1s 空间限制:256MB 限定语言:不限

题目描述

某个产品的RESTfulAPI集合部署在服务器集群的多个节点上,
近期对客户端访问日志进行了采集,需要统计各个API的访问频次根据热点信息在服务器节点之间做负载均衡,
现在需要实现热点信息统计查询功能。RESTfulAPI的由多个层级构成,
层级之间使用/连接,如/A/B/C/D这个地址,A属于第一级,B属于第二级,C属于第三级,D属于第四级。
现在负载均衡模块需要知道给定层级的上某个各字出现的频次,未出现过用0次表示,实现这个功能。
输入描述第一行为N,表示访问历史日志的条数,0<N≤100。
接下来N行,每一行为一个RESTfulAPI的URL地址,约束地址中仅包含英文字母和连接符,最大层级为10,每层级字符串最大长度为10。
最后一行是层级L和要查询的关键词。
输出描述输出给定层级上,关键字出现的频次,使用完全匹配方式(大小写敏感)。

demo



anwser

import sys

input_strs = list()

for line in sys.stdin:
    input_str = line.strip()
    if input_str:
        input_strs.append(input_str)

n = int(input_strs[0])
urls = input_strs[1: 1+n]
level_key: str = input_strs[1+n]
level, key = level_key.split()
level = int(level)

source = dict()
for url in urls:
    names = url.split('/')
    for index in range(len(names)):
        name = names[index]
        if name:
            if index in source:
                if name in source[index]:
                    source[index][name] += 1
                else:
                    source[index][name] = 1
            else:
                source[index] = dict()
                source[index][name] = 1

if level not in source:
    print(0)
elif key not in source[level]:
    print(0)
else:
    print(source[level][key])

=======================================================

CPU算力分配

```

### question

### demo

### anwser

```python
import sys

def main():
    input_strs = list()

    for line in sys.stdin:
        input_str = line.strip()
        if input_str:
            input_strs.append(input_str)

    A_cpu_cnt, B_cpu_cnt = [int(i) for i in input_strs[0].split()]
    A_cpu_list = [int(i) for i in input_strs[1].split()]
    B_cpu_list = [int(i) for i in input_strs[2].split()]
    A_cpu_list.sort()
    B_cpu_list.sort()

    A_sum = sum(A_cpu_list)
    B_sum = sum(B_cpu_list)

    for a in A_cpu_list:
        for b in B_cpu_list:
            if A_sum + b -a == B_sum + a - b:
                print(a, b)
                return

main()

=======================================================

5G网络建设

```

### question

现需要在某城市建设5G网络建设,已经选取N个地点设置5G基站,编号固定为1到N,接下来需要哥哥基站之间使用光纤进行连接, 以确保基站能互联互通,不同基站之间架设光纤的成本各不相同,且有些节点之间已经存在光纤连接。 请你设计算法,计算出能联通这些基站的最小成本是多少。 注意:基站的联通具有传递性,入基站A与基站B架设了光纤,基站B与基站C也架设了光纤, 则基站A与基站C视为可以互相联通。 输入描述: 第一行输入标识基站的个数,其中0 < N <= 20 第二行输入表示具备光纤直连条件的基站对的数目M,其中 0 < M < N*(N - 1) / 2
从第三行开始连续输入M行数据,格式为 X Y Z P, 其中X Y 表示基站的编号, 0 < X <= N, 0 < Y <= N , X != Y , Z 表示 X Y之间架设光纤的成本, 其中 0 < Z <= 100, P 表示是否已经存在光纤连接, 0表示未连接,1表示已连接

输出描述: 如果给定条件,可以建设成功互联互通的5G网络, 则输出最小的建设成本 如果给定条件,无法建设成功互联互通的5G网络,则输出-1


### demo

### anwser

```python
class Edge:
    # 定义边的类
    def __init__(self, u, v, cost, pre):
        self.u = u  # 基站u
        self.v = v  # 基站v
        self.cost = cost  # 架设光纤的成本
        self.pre = pre  # 是否已存在光纤连接

def find(x):
    # 并查集查找函数,用于查找x所在的集合
    if parent[x] != x:
        # 如果x不是自己的父节点,那么就让x的父节点为x的父节点的父节点(路径压缩)
        parent[x] = find(parent[x])
    return parent[x]  # 返回x的父节点

def union(x, y):
    # 并查集合并函数,用于合并x和y所在的集合
    rootX = find(x)  # 找到x的根节点
    rootY = find(y)  # 找到y的根节点
    if rootX != rootY:
        # 如果x和y的根节点不同,那么就将x的根节点的父节点设为y的根节点
        parent[rootX] = rootY

if __name__ == "__main__":
    N = int(input())  # 输入基站的个数
    M = int(input())  # 输入具备光纤直连条件的基站对的数目
    parent = [i for i in range(N + 1)]  # 初始化并查集数组,初始时每个节点的父节点就是自己
    edges = []  # 存储所有的边

    for _ in range(M):
        X, Y, Z, P = map(int, input().split())  # 输入基站X, Y, 架设光纤的成本Z, 是否已存在光纤连接P
        edges.append(Edge(X, Y, Z, P))  # 添加边
        if P == 1:  # 如果已存在光纤连接,那么就将X和Y合并
            union(X, Y)

    # 将所有的边按照成本从小到大排序
    edges.sort(key=lambda edge: edge.cost)
    cost = 0  # 总的成本

    for edge in edges:
        # 如果边的两个端点不在同一个集合中,那么就将这条边添加到最小生成树中
        if find(edge.u) != find(edge.v):
            cost += edge.cost  # 累加成本
            union(edge.u, edge.v)  # 合并边的两个端点所在的集合

    for i in range(2, N + 1):
        # 检查所有的基站是否都在同一个集合中
        if find(i) != find(1):
            # 如果有基站不在同一个集合中,那么就输出-1并结束程序
            print(-1)
            break
    else:
        # 输出总的成本
        print(cost)

=======================================================

title

```

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser
`=======================================================`


## title

### question

### demo

### anwser

`` =======================================================`

results matching ""

    No results matching ""