Google Code Jam 2008 资格赛的第一题:Saving the Universe。

问题描述如下:

Problem

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, NN 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: Y

where 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 < N ≤ 20

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.

—————————————————————————————————————————————————————————————————————————————

简言之,就是进行N轮测试,每次测试先给出S个搜索引擎的名字,再给出Q条搜索内容(都是搜索引擎的名字);

为了不让搜索引擎搜索自己的名字(因为这样会爆炸),系统需要切换搜索引擎,给出每次的测试需要切换搜索引擎的最小次数。

我的想法主要是使用map<string,int>保存每次读取的名字,map对于Key相同的元素只会保存一个。

假如在插入“query”后map的大小等于了搜索引擎的数量S,表明前S-1个种类的名字应该由当前名字("query")的搜索引擎来搜索。这时候系统就把搜索引擎从“query”切换成另一个搜索引擎,清除map并插入“query”,然后循环进行。

后来看到Google官方给出的答案思路也是类似的,只是用了set<string>来保存元素,每次操作会先在set中查找该元素的位置。

下面是我用C++的解答。

#include<iostream>
#include<fstream>
#include<string>
#include<map> using namespace std; int main(){
ifstream readFile("A-large-practice.in");
if (!readFile){
cout << "Open file failed!" << endl;
return 0;
}
ofstream ofs("A-large-pratice.out");
int N; //the number of cases
int S; //the number of search engines
readFile >> N;
int* count = new int[N]; //store results for (int i = 0; i < N; i++){
//read in search engines' name
(readFile >> S).get(); for (int i = 0; i < S; i++){
string engine;
getline(readFile, engine);
} int Q;
(readFile >> Q).get(); //the number of incoming queries
map<string,int> find_list;
int switch_count = 0;
find_list.clear();
for (int j = 0; j < Q; j++){
string query;
getline(readFile, query);
find_list[query] = 1;
if (find_list.size() == S) { //list is full of all kinds of engines
find_list.clear();
find_list[query] = 1;
switch_count++; //It's one switch
}
}
count[i] = switch_count;
}
for (int i = 0; i < N; i++){
ofs << "Case #" << i + 1 << ": " << count[i] << endl;
} delete[]count;
readFile.close();
<span style="white-space:pre"> </span>ofs.close();
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[C++]Saving the Universe——Google Code Jam Qualification Round 2008的更多相关文章

  1. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  2. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  3. [Google Code Jam (Qualification Round 2014) ] A. Magic Trick

    Problem A. Magic Trick Small input6 points You have solved this input set.   Note: To advance to the ...

  4. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  5. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  6. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  7. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  8. Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle

    Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...

  9. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

随机推荐

  1. asp.net 连接sqlserver数据库

    在asp.net中连接sqlserver数据库之前,首先得确保正常安装了sqlserver2008,同时有数据库. 在项目中添加一个类DB,用来专门负责执行对数据库的增删改查.在添加的过程中会弹出下面 ...

  2. “error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载)

    解决方案: “error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载) 遇到的问题: 在.h头文件中采用namespace 命名空间报错 test.h namespace LMR ...

  3. 将控制台程序做成windows服务

    一:添加windows服务 二:修改XXXInstaller1的StartType=Automatic,修改ProcessInstaller1的Account=LocalSystem 三:在progr ...

  4. 安装虚拟机VMWare时出现1021错误的解决办法

    今天安装虚拟机(VMWare Workstation9.0),中途老是出现错误:Failed to create the requested registry key key installer er ...

  5. codeforces 10 D. LCIS LCIS O(n^2)算法

    题目链接 给出两个序列, 求出他们的最长公共上升子序列. 两层循环, 内层循环j, 外层i. 如果a[i] == b[j], 那么dp[j] = max(dp[j], dp[best]+1), bes ...

  6. 密码学——网间数据加密传输全流程(SSL加密原理)

    0.导言 昨天写了一篇关于<秘钥与公钥>的文章,写的比较简单好理解,有点儿像过家家,如果详细探究起来会有不少出入,今天就来详细的说明一下数据加密的原理和过程.这个原理就是大名鼎鼎SSL的加 ...

  7. sim卡中电话本(ADN)的简要格式

    ADN的格式 ADN存放于sim卡下面3f00/7f10/6f3a,记录文件格式,其最小记录格式为14,最长为255(?),记录个数最大为255(?) 其后数14个字节是必有的,其前12个字节是电话号 ...

  8. 微软Windows 7 “可启动U盘”制作工具及使用方法,非常的简单

    目前,用“可启动U盘”替代光驱光盘安装操作系统,已经成为一种时尚(至少对没有刻录机或不愿购买光碟的群体是这样).制作“可启动U盘”的方法和工具很多,区别无非是制作的难易程度和对“U盘类型”的支持程度. ...

  9. qtcreator增加doxygen注释

    1,在以下网址找到和你的qt creator版本相对应的插件文件,还要注意系统版本 http://dev.kofee.org/projects/qtcreator-doxygen/files 2,根据 ...

  10. Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)

    周末天冷,索性把电脑抱到床上上网,这几天看了 dbzhang800 博客关于 Qt 事件循环的几篇 Blog,发现自己对 Qt 的事件循环有不少误解.从来只看到现象,这次借 dbzhang800 的博 ...