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, TT 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, TT 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, TT 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总结的更多相关文章

  1. 【面试题】Round A China New Grad Test 2014总结

    我也有够懒的,今天才跑来写总结,自觉面壁中… 上一篇是Practice Round,今天是Round A,五道题. 每次做完都想说,其实题不难..但在做的过程中总是会各种卡,只有自己一行一行实现了,才 ...

  2. Practice Round China New Grad Test 2014 报告

    今天有Google of Greater China Test for New Grads of 2014的练习赛,主要是为了过几天的校园招聘测试做练习用的,帮助熟悉平台,题目嘛,个人觉得除了A题外, ...

  3. Google开发者大会:你不得不知的Tensorflow小技巧

    Google开发者大会:你不得不知的Tensorflow小技巧 同步滚动:开   Google Development Days China 2018近日在中国召开了.非常遗憾,小编因为不可抗性因素滞 ...

  4. 谷歌 google

    google Google是搜索引擎名,也是一家美国上市公司名称.Google公司于1998年9月7日以私有股份公司的形式创立,以设计并管理一个互联网的搜索引擎.Google公司的总部称作“Googl ...

  5. 每日英语:U.S. Media Firms Stymied in China

    China's recent clampdown on foreign media is crimping the expansion plans of Western news organizati ...

  6. J2EE面试题集锦_

    一.基础问答 不能被继承的类有[Long  Double  Float  Short  Void  Class  Math  String] 他们都被final修饰         类可以被继承[Th ...

  7. Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) 原文链接  译文链接 译者: 沈义扬,罗立树,何一昕,武祖  校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...

  8. Java面试题相关内容

    选择题(共5题,每题1.5分,共75分.多选题选不全或选错都不得分.)1. 以下属于面向对象的特征的是(C,D).(两项)A) 重载B) 重写C) 封装D) 继承 2. 以下代码运行输出是(C)pub ...

  9. 阿里和Google的JAVA开发规约

    <阿里 JAVA开发规约> 阿里巴巴Java开发手册终极版v1.3.0.pdf 出处:github地址:https://github.com/alibaba/p3c <Google ...

随机推荐

  1. 多线程基本概论multithread

    多线程 基本概念 进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 通过 活动监视器 可以查看 Mac 系统中所开启的进程 线程 进程要想 ...

  2. 刀哥多线程之并发队列gcd-05-dispatch_queue_concurrent

    并发队列 特点 以先进先出的方式,并发调度队列中的任务执行 如果当前调度的任务是同步执行的,会等待任务执行完成后,再调度后续的任务 如果当前调度的任务是异步执行的,同时底层线程池有可用的线程资源,会再 ...

  3. 整齐地输出n的平方,立方

    初学C语言,有许多搞不明白的地方.编程,最重要的就是实践.今天,我偶然间看到书上的练习,做了一个能整齐地输出n,n的平方,n的立方的小程序.首先,我先用伪代码设计程序: 提示用户输入表格上限,下限或退 ...

  4. [整理]Selector、shape详解

    Selector.shape详解(一) Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> ...

  5. 关于table元素的认识

    表格是网页上最常见的元素,但是,现在对很多刚入行的前端们那是谈table色变.那是为啥?这是表格的框架的简单.明了.在传统的网页中使用没有边框的表格来排版是非常流行.在web标准逐渐深入设计领域以后, ...

  6. python中的小技巧

    1.求1~100以内的素数 prime=filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,101))#列表推导,一行搞 ...

  7. ios各种手势,很有意思

    转自http://blog.csdn.net/likendsl/article/details/7554150 这哥们很厉害的 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由U ...

  8. (转)android Fragments详解三:实现Fragment的界面

    为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中.    一个 要为fragment提供layo ...

  9. jQuery插件实例一:年华时代插件Alert对话框

    在工作中使用jQuery插件相信对于程序员来说非常普遍,在网络上也有很多优秀的插件可供大家使用,功能非常强大.在之前用过的一些插件中,有些太过追求功能的强大和可配置性,造成使用的复杂度上升.个人认为与 ...

  10. Jsp实现form的file和text传递(multipart/form-data)

    Jsp实现form的file和text传递(multipart/form-data) 首先是form部分,因为要有<input type="file" />的类型,所以 ...