Code jam 2008 practice A

Code jam 2008 practice A

  • A:Saving the universe
  • B:Train timetable
  • C:Fly swatter

A:

The urban legend goes that if you go to the Google homepage and search for “Google”, the universe will implode. We have a secret to share… It is true! Please don’t try it, or tell anyone. All right, maybe not. We are just kidding.

The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine’s name, the universe does implode!

To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they’re received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.

Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.

有很多搜索引擎,当请求到某一个搜索引擎自己的时候,就会发生崩溃,所以需要很好的调度这些搜索引擎。现在将所有的搜索引擎都放在一起,彼此服务器之间可以相互指定,这时候就发生了交换。现在给出例子和数据集,需要给出解决方案,计算在满足请求的情况下,最少发生了几次服务器之间的交换。

  • 输入:
    第一行是测试样例的数量
    第二行是搜索引擎的数量
    接下来是对应的搜索引擎名字
    再接着是请求的数量
    最后是对应的具体的请求的名字
  • 输出:
    对于每一个测试样例,格式如下:
    case #x:y
    表示的是对于第x个样例,发生了y次交换

分析

把所有的对于服务器的请求进行分段,要求被分段的请求不包含已经给定了的所有的服务器,否则包含了所有的服务器之后,就需要发生交换。这里用到了贪心算法的思想。

代码

import os
os.system('cls')

# solution函数每次解决单个测试样例
def solution(a):
    # 每次读取一行,读取完毕测试样例的数量之后,立即使用readline读取搜索引擎的数量即可
    search_engine_num = int(a.readline())
    # print(search_engine_num, end=" ")
    switch_num = 0
    i = 1
    # 只需要知道搜索引擎的数量即可,这里利用readline跳过搜索引擎,方便继续读取
    while i <= search_engine_num:
        a.readline()
        i += 1
    query_num = int(a.readline())
    query_seg = {}
    for i in range(query_num):
        query = str(a.readline().strip('\n'))
        if query_seg.get(query, 0) == 0:
            # 长度再增加势必引起崩溃,必须开始交换
            if len(query_seg) == search_engine_num - 1:
                switch_num += 1
                query_seg.clear()
            # 判断用了长度减一,将会跳过下一个搜索引擎,所以把被忽略的一个引擎添加到请求的段中
            query_seg[query] = 1
    return switch_num

# infile = open("A-small-practice.in", "r")
infile = open("A-large-practice.in", "r")
caseNum = int(infile.readline())
result = []
flag = 1
# os.remove('test_result.txt')
if os.path.exists("A_large_result.out"):
    os.remove("A_large_result.out")
while flag <= caseNum:
    switch_num = solution(infile)
    print("Case #", flag, ":", switch_num)
    with open("A_large_result.out", 'a') as f:
        f.writelines(["Case #", str(flag), ": ", str(switch_num), "\n"])
        f.close
    flag += 1

结果 (大数据集):

Case #1: 0
Case #2: 0
Case #3: 1
Case #4: 1
Case #5: 3
Case #6: 3
Case #7: 999
Case #8: 498
Case #9: 12
Case #10: 1
Case #11: 4
Case #12: 1
Case #13: 6
Case #14: 0
Case #15: 1
Case #16: 11
Case #17: 1
Case #18: 3
Case #19: 3
Case #20: 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值