【面试题】Google of Greater China Test for New Grads of 2014总结
2014年Google中国校园招聘采用在线技术笔试,在Code Jam平台上,14号9点到15号9点开放测试题,帮助大家熟悉环境。这个周末也有够忙的,当时就上去看了一下,把输入文件下了一下,今天才把题目做了,对于我这个EE到CS的水军来说,通过做这个测试还是学到了很多东西的,值得总结一下。
其实三道题都不是很难,至少逻辑不算难,但是实现起来我就发现我真的是一只小小小菜鸟…要学的东西实在是太多太多~
先说第一题吧,在这三道题里面算相对难的了
Problem A. Bad Horse
Problem
As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself. That what he's got you -- his loyal henchman -- for.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a positive integer M on a line by itself -- the number of troublesome pairs of League members. The next M lines each contain a pair of names, separated by a single space.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.
Limits
1 ≤ T ≤ 100.
Each member name will consist of only letters and the underscore character.
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.
Small dataset
1 ≤ M ≤ 10.
Large dataset
1 ≤ M ≤ 100.
Sample
| Input | Output |
| 2 1 Dead_Bowie Fake_Thomas_Jefferson 3 Dead_Bowie Fake_Thomas_Jefferson Fake_Thomas_Jefferson Fury_Leika Fury_Leika Dead_Bowie |
Case #1: Yes Case #2: No |
我会说,我着实不喜欢这些个英文描述吗。。理解问题就理解了半天…最终发现就是一个用染色就可以解决的问题。
思路倒是很容易有,但是实现起来…
读写文件就把我这个菜鸟卡了好一会儿,搜了一下发现其实还蛮简单的,比我想象的要简单很多,具体就看代码吧。
有了数据了,然后就是处理数据。因为是菜鸟,所以,所有的结构我都是用数组自己去实现的…先是把每个名字对应一个编号,每次一个新名字都要花O(n)的时间复杂度去查找是不是已经存在了,好在数据量很小,一个case里面最多也就20个名字,(或者200个,总之对计算机来说是很小的)。然后,建立一个二维数组,相当于把这些人之间的关系建立起来,这样,把数据遍历一遍就可以建起来这个结构。然后再用染色的方法去处理这个二维数组就好。
后来看到有人用map建立名字和编号的对应关系,这样查找复杂度就是O(log n)了,代码也简单了很多,之前没用过,这次学习了。
另外,我建立二维数组大小都是固定的,20*20,看到有人用vector,可以相当于动态数组,学习了。
所以,看到这些之后,为了能真正理解,我把之前的代码基于这两个结构重新实现了一遍。代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std; bool IsTwoLeague(vector<int> graph[], int *league, int index)
{
for(int i = ; i < graph[index].size(); ++i)
{
int nameNum = graph[index][i];
if(league[nameNum] == -)
{
league[nameNum] = - league[index];
if(!IsTwoLeague(graph, league, nameNum))
return false;
}
else if(league[nameNum] == league[index])
return false;
}
return true;
} void AddNameToMap(map<string, int> &nameMap, string s)
{
if(!nameMap.count(s))
nameMap.insert(pair<string, int>(s, nameMap.size()));
} void BadHorse()
{
ifstream fin("A-small-1-attempt0.in");
ofstream fout("A-small-1-attempt0.out");
int numOfCases = ;
fin >> numOfCases;
for(int i = ; i <= numOfCases; ++i)
{
vector<int> graph[];
map<string, int> nameMap;
int caseNum = ;
fin >> caseNum;
for(int i = ; i < caseNum; ++i)
{
string name1, name2;
fin >> name1 >> name2;
AddNameToMap(nameMap, name1);
AddNameToMap(nameMap, name2);
graph[nameMap[name1]].push_back(nameMap[name2]);
graph[nameMap[name2]].push_back(nameMap[name1]);
}
int league[];
memset(league, -, sizeof(league));
int numOfNames = nameMap.size();
bool rel = true;
for(int i = ; i < numOfNames; ++i)
{
if(league[i] == -)
{
league[i] = ;
rel = IsTwoLeague(graph, league, i);
if(!rel)
break;
}
}
if(rel)
fout << "Case #" << i << ": " << "Yes" << endl;
else
fout << "Case #" << i << ": " << "No" << endl;
}
fin.close();
fout.close();
} int main()
{
BadHorse();
}
Problem B. Captain Hammer
Problem
The Hamjet is a true marvel of aircraft engineering. It is a jet airplane with a single engine so powerful that it burns all of its fuel instantly during takeoff. The Hamjet doesn't have any wings because who needs them when the fuselage is made of a special Wonderflonium isotope that makes it impervious to harm.
Piloting the Hamjet is a not a job for your typical, meek-bodied superhero. That's why the Hamjet belongs to Captain Hammer, who is himself impervious to harm. The G-forces that the pilot endures when taking a trip in the Hamjet are legen-dary.
The Hamjet takes off at an angle of θ degrees up and a speed of V meters per second. Vis a fixed value that is determined by the awesome power of the Hamjet engine and the capacity of its fuel tank. The destination is D meters away. Your job is to program the Hamjet's computer to calculate θ given V and D.
Fortunately, the Hamjet's Wondeflonium hull is impervious to air friction. Even more fortunately, the Hamjet doesn't fly too far or too high, so you can assume that the Earth is flat, and that the acceleration due to gravity is a constant 9.8 m/s2 down.
Input
The first line of the input gives the number of test cases, T. T lines follow. Each line will contain two positive integers -- V and D.
Output
For each test case, output one line containing "Case #x: θ", where x is the case number (starting from 1) and θ is in degrees up from the the horizontal. If there are several possible answers, output the smallest positive one.
An answer will be considered correct if it is within 10-6 of the exact answer, in absolute or relative error. See the FAQ for an explanation of what that means, and what formats of floating-point numbers we accept.
Limits
1 ≤ T ≤ 4500;
1 ≤ V ≤ 300;
1 ≤ D ≤ 10000;
It is guaranteed that each test case will be solvable.
Sample
| Input | Output |
| 3 98 980 98 490 299 1234 |
Case #1: 45.0000000 Case #2: 15.0000000 Case #3: 3.8870928 |
这个题其实就是一道很简单的物理题…有了上一题的成长,这一题写起来着实没啥难度,大家都是理工科出身,公式我就不列了,你们懂的。
这道题里面学习到的一个小点就是,cout 在输出 float 或者 double 的时候,对于 45.0000000 这种数它只显示 45,如果要想固定精度,需要设置一下
fout << "Case #" << i << ": " << fixed << setprecision(7) << theta << endl;其中 setprecision 需要包含头文件 #include <iomanip>。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std; void CaptainHammer()
{
ifstream fin("B-small-attempt0.in");
ofstream fout("B-small-attempt0.out");
double g = 9.8;
double pi = acos(-1.0);
int numOfCases = ;
fin >> numOfCases;
for(int i = ; i <= numOfCases; ++i)
{
int v, d;
fin >> v >> d;
double theta = asin(g * d / (v * v)) / ;
theta = theta * / pi;
fout << "Case #" << i << ": " << fixed << setprecision() << theta << endl;
}
fin.close();
fout.close();
} int main()
{
CaptainHammer();
}
Problem C. Moist
Problem
Moist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabetical order, so that he can find the cards that he wants on short notice whenever it is necessary.
The problem is -- Moist can't actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.
Moist has figured out that the robot's sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card, it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.
As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to sort his deck of cards.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each one starts with a line containing a single integer, N. The next N lines each contain the name of a figure skater, in order from the top of the deck to the bottom.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of trading cards.
Limits
1 ≤ T ≤ 100.
Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 100.
Sample
| Input | Output |
| 2 2 Oksana Baiul Michelle Kwan 3 Elvis Stojko Evgeni Plushenko Kristi Yamaguchi |
Case #1: 1 Case #2: 0 |
我会说这道题我其实没看太懂吗。。。我的理解就是,对于一个 case,从上往下扫描,遇到一个不合排序法则的,就把已经扫描的这部分排序,付$1。比如
c d b a
扫到 b,发现不对,应该变为 b c d,付$1,然后,再碰到 a,又不对,应该是 a b c d,付$1。这个 case 就是$2。
这样的话,只需要维护一个扫描过的这部分的最大字符串就好了,碰到比这个字符串大的就更新这个字符串,碰到比这个字符串小的说明顺序有问题,dollar++。对于上面的例子,max = c, dollar = 0; d > max, max = d; b < max, dollar++; a < d, dollar++。我觉得这样有点太简单,所以一直怀疑我理解错了……
按我的理解的话,代码如下:
#include <iostream>
#include <fstream>
#include <string>
using namespace std; void Moist()
{
ifstream fin("C-small-1-attempt0.in");
ofstream fout("C-small-1-attempt0.out");
int numOfCases = ;
fin >> numOfCases;
for(int i = ; i <= numOfCases; ++i)
{
int caseNum = ;
fin >> caseNum;
int dollars = ;
string sMax;
string s;
getline(fin, s);
for(int i = ; i <= caseNum; ++i)
{
getline(fin, s);
if(s < sMax)
++dollars;
else if(s > sMax)
sMax = s;
}
fout << "Case #" << i << ": " << dollars << endl;
}
fin.close();
fout.close();
} int main()
{
Moist();
}
总之,还是学到了很多东西的,我这只小小小菜鸟要继续努力。
【面试题】Google of Greater China Test for New Grads of 2014总结的更多相关文章
- 【面试题】Round A China New Grad Test 2014总结
我也有够懒的,今天才跑来写总结,自觉面壁中… 上一篇是Practice Round,今天是Round A,五道题. 每次做完都想说,其实题不难..但在做的过程中总是会各种卡,只有自己一行一行实现了,才 ...
- Practice Round China New Grad Test 2014 报告
今天有Google of Greater China Test for New Grads of 2014的练习赛,主要是为了过几天的校园招聘测试做练习用的,帮助熟悉平台,题目嘛,个人觉得除了A题外, ...
- Google开发者大会:你不得不知的Tensorflow小技巧
Google开发者大会:你不得不知的Tensorflow小技巧 同步滚动:开 Google Development Days China 2018近日在中国召开了.非常遗憾,小编因为不可抗性因素滞 ...
- 谷歌 google
google Google是搜索引擎名,也是一家美国上市公司名称.Google公司于1998年9月7日以私有股份公司的形式创立,以设计并管理一个互联网的搜索引擎.Google公司的总部称作“Googl ...
- 每日英语:U.S. Media Firms Stymied in China
China's recent clampdown on foreign media is crimping the expansion plans of Western news organizati ...
- J2EE面试题集锦_
一.基础问答 不能被继承的类有[Long Double Float Short Void Class Math String] 他们都被final修饰 类可以被继承[Th ...
- Google Guava官方教程(中文版)
Google Guava官方教程(中文版) 原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...
- Java面试题相关内容
选择题(共5题,每题1.5分,共75分.多选题选不全或选错都不得分.)1. 以下属于面向对象的特征的是(C,D).(两项)A) 重载B) 重写C) 封装D) 继承 2. 以下代码运行输出是(C)pub ...
- 阿里和Google的JAVA开发规约
<阿里 JAVA开发规约> 阿里巴巴Java开发手册终极版v1.3.0.pdf 出处:github地址:https://github.com/alibaba/p3c <Google ...
随机推荐
- JS中的函数,Array对象,for-in语句,with语句,自定义对象,Prototype
一)函数 A)JS中的函数的定义格式: function add(a,b) { var sum = a+b; document.write("两个数的和是:" + sum); // ...
- Box of Bricks最小移动砖块数目
Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and ...
- SQL0294N 容器已在使用中。 SQLSTATE=42730
在建立数据库后,建立表空间时,出现如下错误: CREATE TABLESPACE TABLESAPCE_NAME PAGESIZE 32K MANAGED BY SYSTEM USING ('E:\D ...
- AppCan移动技术全景图:创新、协作、支撑
开发者是移动互联网宏伟蓝图的最终实现者.如果你有创意.有技术,你可以开发一款服务上亿人的应用.所以,我感觉幸运,没有任何一个时代,能像现在这么好,技术人能够服务这么广大的市场,能够撬动百亿级的市场. ...
- AppCan移动平台开发常见问题解答
在使用AppCan移动平台开发跨平台APP时,有开发者会遇到一些问题, 不急,跟笔者一起来聊一聊使用AppCan平台开发中常见问题的解答方法. 问1.正常是按照官网提供的4个iphone启动图尺寸来做 ...
- android开发系列之回调函数
想必对于回调函数大家肯定不陌生,因为这是我们开发里面常用的代码技巧.我也就不废话了,让我们直接来看代码吧! public class TestCallback { public interface I ...
- 黑客群体的露面说明互联网公司开始回馈IT行业了,
揭开中国黑客群体的神秘面纱 年薪数百万 2015-04-26 09:59:45 15259 次阅读 14 次推荐 稿源:经济观察报 33 条评论 在网络世界有专属的代号,那里才是他们最习惯的“世界 ...
- 3.Knockout.Js(属性绑定)
前言 让visible绑定到DOM元素上,使得该元素的hidden或visible取决于绑定的值. 简单的绑定 首先还是先定义一个ViewModel var AppViewModel = { shou ...
- org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
- 使用usb+preseed在virtualbox上安装ubuntu(一)
1.制作usb boot盘,在ubuntu上使用startup disk creater将ubuntu-server12.04.iso写入到usb中: 2.修改syslinux文件夹中的syslinu ...