[C++]Saving the Universe——Google Code Jam Qualification Round 2008
来源:互联网 发布:工业手持数据采集终端 编辑:程序博客网 时间:2024/05/18 08:41
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, 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 < N ≤ 20
Small dataset
2 ≤ S ≤ 10
0 ≤ Q ≤ 100
Large dataset
2 ≤ S ≤ 100
0 ≤ Q ≤ 1000
Sample
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 casesint S;//the number of search enginesreadFile >> N;int* count = new int[N];//store resultsfor (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 queriesmap<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 enginesfind_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
- google code jam 2008 Saving the Universe
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
- [C++]Standing Ovation——Google Code Jam 2015 Qualification Round
- [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round
- Google Code Jam 2010 Qualification Round 2012 Problem B && C
- Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master
- Google Code Jam 2015 Qualification Round: Problem C. Dijkstra
- 2009 Google Code Jam Qualification Round 题解
- Google Code Jam 2011 Qualification Round ProblemA
- Google Code Jam 2012 Qualification Round
- Google Code Jam Qualification Round 2012
- Google Code Jam 2010 is in Dublin!——Qualification Round
- google code jam 2011 Qualification Round 资格赛 Problem C. Candy Splitting
- Reverse-GOOGLE CODE JAM AFICA 2010 Qualification Round
- T9 Spelling-GOOGLE CODE JAM AFICA 2010 Qualification Round
- Store Credit-GOOGLE CODE JAM AFICA 2010 Qualification Round
- ACM-GOOGLE CODE JAM 2011 Qualification Round 2011(总结)
- 《Effective C++》学习笔记——条款29
- 原生ajax
- 设置jvm内存与tomcat内存方法
- java web之Listener实现单态登录
- Floyd算法(所有点对最短路径)
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
- 多线程问题及处理
- 一切小知识点,可能不知道原因
- 第三单元3.22作业
- 为什么Java字符串是不可变对象
- 简析TCP的三次握手与四次分手
- POI教程
- 15第三周项目四——考了语文数学的学生
- 学习笔记6-面向对象