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. 《高性能javascript》读书笔记:P1减少跨作用域的变量访问

    前端优化,有两个意义:1.为了让用户在浏览网页时获得更好的体验 2.降低服务器端的访问压力,节省网络流量. 除了换个好主机连上个千兆网这样的硬件问题,优化部分的实现方式目前也大致两种,一种是页面级别的 ...

  2. mysql索引合并:一条sql可以使用多个索引

    前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...

  3. yum代理设置

    vi /etc/yum.conf 加入以下:proxy=http://代理服务器ip:port 如果代理需要账号密码:proxy_username=userproxy_password=密码

  4. 史上最佳 Mac+PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境的配置

    在上一篇 PHP 系列的文章<PHP 集成开发环境比较>中,我根据自己的亲身体验,非常简略的介绍和对比了几款常用的集成开发环境,就我个人而言,比较推崇 Zend Studio 和 PhpS ...

  5. HMMPfam的安装使用手记(转载)

    转载至:http://blog.sina.com.cn/s/blog_3f6403290100rb61.html(感谢原文作者) HMMPfam的安装使用手记前言 简要介绍一下 HMMPfam吧.这还 ...

  6. java implement

    接口不能被实例化,但是可以声明一个接口类型的变量. eg. A implements B,则可以有B variableName = new A(),这和extends的用法是类似的 接口可被认为是纯抽 ...

  7. cocos2dx 3.x中的渲染机制

    1.由2.x的渲染节点,变成添加渲染命令,可以避免重复渲染相同的节点,提高了渲染效率 2.单机游戏通常要求apk包在30M以内,没压缩1M会有1%的转换率(下载转换率),即收入会提高 3.2.x中首先 ...

  8. PHP URL 重定向 的三种方法(转载)

    为了方便查询,转载一篇. 1.使用header()函数    PHP的HTTP相关函数种提供了一个 header()函数,首先要清楚,header()函数必须放在php程序的开头部分,而且之前不能有另 ...

  9. loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写

    使用loadrunner进行压力测试主要分两步,第一步是编写脚本(比较重点),第二步执行测试(配置都是在界面上点点就行了,当然我只的是比较简单的,能满足日常需要的),第三步分析结果(这一步比较高深,但 ...

  10. ORACLE 检查数据库表中是否存在不规范字 段的语句参考.sql

    --查看是否有除number,char,date,varchar2,clob/blob之外的类型,比如:NVARCHAR2,TIMESTAMP(6),FLOATSELECT DISTINCT a.DA ...