免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1449 | 回复: 0
打印 上一主题 下一主题

Practice-Saving the Universe [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-19 23:30 |只看该作者 |倒序浏览
n the practice contest, you may try as many times as you want.Small input
5 points

Download A-small.inMore options   ▼SubmitLarge input
20 points

Download A-large.inMore options   ▼SubmitProblem
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.
Input
The first line of the input file contains the number of cases, N. N test cases follow.
Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.
The following line contains a number Q -- the number of incoming queries. The next Qlines will each contain a query. Each query will be the name of a search engine in the case.
Output
For each input case, you should output:
Case #X: Ywhere X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.
Limits
0 Small dataset
2 ≤ S ≤ 10
0 ≤ Q ≤ 100
Large dataset
2 ≤ S ≤ 100
0 ≤ Q ≤ 1000
Sample
Input
Output
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol
Case #1: 1
Case #2: 0
In the first case, one possible solution is to start by using Dont Ask, and switch to NSM after query number 8.
For the second case, you can use B9, and not need to make any switches.
               
               
               
                import os
FileIn=open("A-large-practice.in","r")
caseNumber=FileIn.readline()
caseNumber=int(caseNumber)
#print caseNumber
for i in range(caseNumber):
    sNumber=int(FileIn.readline())
    s=[]
    sA=[]
    for j in range(sNumber):
        s.append(FileIn.readline().strip())
   
    qNumber=int(FileIn.readline())
    q=[]
    qS=[]
    for k in range(qNumber):
        t=FileIn.readline().strip()
        q.append(t)
        qS.append(s.index(t))
    #print s
    #print q
    #print qS
    sA=[0 for x in range(sNumber)]
    sCounter=0
    switchCounter=0
    for k in range(qNumber):
        if sA[qS[k]]==0 :
            sCounter=sCounter+1
            sA[qS[k]]=1
            if(sCounter==sNumber):
                switchCounter=switchCounter+1
                t=qS[k]
                #print t
                sA=[0 for x in range(sNumber)]
                sCounter=0
    if switchCounter==0:
        
        print "Case #"+str(i+1)+": "+str(switchCounter)
    else:
        print "Case #"+str(i+1)+": "+str(switchCounter-1)


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29089/showart_2032314.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP