Google Round A China New Grad Test 2014

谷歌校招第一次机试结果:https://code.google.com/codejam/contest/2924486/scoreboard#

可以从上述网址查询结果,也可以下载大牛的代码,看看大牛的编程思路。

2013.9.22 当天早上断断续续做了一上午,只做出来两道题,学渣路 漫漫其修远兮~ 


Problem A. Read Phone Number

Problem:

Do you know how to read the phone numbers inEnglish? Now let me tell you.

For example, In China, the phone numbers are11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format,i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e.150 122 33444. Different formats lead to different ways to read these numbers:

150 1223 3444 reads one five zero one doubletwo three three triple four.

150 122 33444 reads one five zero one doubletwo double three triple four.

Here comes the problem:

Given a list of phone numbers and thedividing formats, output the right ways to read these numbers.

Rules:

Single numbers just read them separately.

2 successive numbers use double.

3 successive numbers use triple.

4 successive numbers use quadruple.

5 successive numbers use quintuple.

6 successive numbers use sextuple.

7 successive numbers use septuple.

8 successive numbers use octuple.

9 successive numbers use nonuple.

10 successive numbers usedecuple.

More than 10 successivenumbers read them all separately.

Input:

The first line of the input gives the numberof test cases, T. T lines|test cases follow. Each line containsa phone number N and the dividing format F, one or more positiveintegers separated by dashes (-), without leading zeros and whose sum alwaysequals the number of digits in the phone number.

Output

For each test case, output one linecontaining "Case #x: y", where x is the case number (starting from 1)and y is the reading sentence in English whose words are separated by a space.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ length of N ≤10.

Large dataset

1 ≤ length of N ≤100.

Sample:


Input 
 

3

15012233444 3-4-4

15012233444 3-3-5

12223 2-3


Output :

Case #1: one five zero one double two three three triple four

Case #2: one five zero one double two double three triple four

Case #3: one two double two three

string number[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string outstr[10] = {"", "double", "triple", "quadruple", "quintuple", "sextuple", "septuple", "octuple", "nonuple", "decuple"};
void outfile(int pos, int repeat)
{
	if (repeat<=10)
		cout<<outstr[repeat-1]<<" "<<number[pos]<<" ";
	else//more than ten
		for (int mten=0;mten<repeat;mten++)
		{
			cout<<number[pos]<<" ";
		}
}
void readPhoneNum()
{
	int N;char pn[1000],rw[1000];//phonenumber and read method
	while(scanf("%d", &N) != EOF)
	{
		for (int cnt=1;cnt<=N;cnt++)
		{
			printf("Case #%d:", cnt);
			cin >> pn >> rw;
			int w(0),readway[1000];
			int num=0;
			for (int r=0;rw[r];r++)
			{
				if (rw[r]=='-')
				{
					readway[w++]=num;
					num=0;
					continue;
				}
				num=num*10+rw[r]-'0';
			}//readway include w numbers
			readway[w]=num;//the last one
			int i=0;
			for (int p=0;pn[p];)
			{
				if (readway[i]==1)
					cout<<number[pn[p]-'0']<<" ";
				else
				{
					int repeat(1);
					for (int k=1;k<readway[i];k++)
					{
						if (pn[p+k]==pn[p+k-1])
							repeat++;
						else
						{
							int pos= pn[p+k-1]-'0';//the latter
							outfile(pos,repeat);
							repeat=1;
						}
						if (k==readway[i]-1)//in the end now
						{
							int pos=pn[p+k]-'0';//the last
							outfile(pos,repeat);
						}
					}
				}
				p=p+readway[i];
				i++;
			}
			cout<<endl;
		}
	}
}

Problem B. Rational Number Tree

Problem

Consider an infinite complete binary treewhere the root node is 1/1 and left and right childs of node p/q are p/(p+q)and (p+q)/q, respectively. This tree looks like:

         1/1
    ______|______
    |           |
   1/2         2/1
 ___|___     ___|___
 |     |     |     |
1/3   3/2   2/3   3/1
...

It is known that every positive rationalnumber appears exactly once in this tree. A level-order traversal of the treeresults in the following array:

1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...

Please solve the following two questions:

Find the n-th element of the array,where n starts from 1. For example, for the input 2, the correctoutput is 1/2.

Given p/q, find its position in thearray. As an example, the input 1/2 results in the output 2.

Input

The first line of the input gives the numberof test cases, T. T test cases follow. Each test case consistsof one line. The line contains a problem id (1 or 2) and one or two additionalintegers:

If the problem id is 1, then only oneinteger n is given, and you are expected to find the n-thelement of the array.

If the problem id is 2, then twointegers p and q are given, and you are expected to findthe position of p/q in the array.

Output

For each test case:

If the problem id is 1, then output one linecontaining "Case #x: p q", where x is the case number(starting from 1), and p, q are numerator and denominator of theasked array element, respectively.

If the problem id is 2, then output one linecontaining "Case #x: n", where x is the case number(starting from 1), and n is the position of the given number.

Limits

1 ≤ T ≤100; p and q are relatively prime.

Small dataset

1 ≤ n, p, q ≤ 2^16-1; p/q isan element in a tree with level number ≤ 16.

Large dataset

1 ≤ n, p, q ≤ 2^64-1; p/q isan element in a tree with level number ≤ 64.

Sample


Input 
 


Output 
 

4
1 2
2 1 2
1 5
2 3 2
Case #1: 1 2
Case #2: 2
Case #3: 3 2
Case #4: 5

此题即对如上所述的二叉树,当输入一个数字时,输出对应数字位置的结果,输入两个数字时,输出该结果在树中的位置。如下为排名第一的dreamoon的代码思路,因为large dataset中最大值已经到2^64-1,所以需要使用unsigned long long类型。

将根节点视为第1个数,当求第N个数时,如果N为偶数,则为左子树;否则为右子树。求N可转化为求N/2,如此递归即可。

求(i/j)的位置,首先如果i>j,则为右子树,i<j为左子树,同样将其上推一层递归即可。

By dreamoon
#define  ULL unsigned long long
ULL two[64];
void go1(int lv,ULL n,ULL& p, ULL& q){
	if(lv==0){
		p=q=1;
		return;
	}
	ULL p2,q2;
	go1(lv-1,n>>1,p2,q2);//获得上一层树的第n/2个结果
	if(n%2==0){//左子树
		p=p2;
		q=p2+q2;
	}
	else{//右子树
		p=p2+q2;
		q=q2;
	}
}
ULL go2(ULL p,ULL q){
	if(p==1&&q==1)return 1;
	if(p<q){
		return go2(p,q-p)*2;
	}
	else{
		return go2(p-q,q)*2+1;
	}
}
int RationalNumberTree()
{ 
	two[0]=1;
	for(int i=1;i<64;i++)
		two[i]=two[i-1]*2;
	int nCase,case_n(1); 
	fin>>nCase;
	while (nCase-- > 0){
		int id; 
		fin>>id;
		if(id==1){//输出对应的第n个结果
			ULL n;
			fin>>n;
			int lv=0;
			while(n>two[lv]){
				n-=two[lv++];
			}//获得n位于树的第lv层的第n个结果
			ULL p,q;
			go1(lv,n-1,p,q);//n-1是因为调用函数时n/2方便
			fout<< "Case #" <<case_n++<< ": ";
			fout<<p<<" "<<q<<endl;
		}
		else{
			fout<< "Case #" <<case_n++<< ": ";
			ULL p,q;
			fin>>p>>q;
			fout<<go2(p,q)<<endl;
		}
	}
	return 0;
}

Problem C. Sorting

Problem:

Alex and Bob are brothers and they bothenjoy reading very much. They have widely different tastes on books so theykeep their own books separately. However, their father thinks it is good topromote exchanges if they can put their books together. Thus he has bought anone-row bookshelf for them today and put all his sons' books on it in randomorder. He labeled each position of the bookshelf the owner of the correspondingbook ('Alex' or 'Bob').

Unfortunately, Alex and Bob went outside anddidn't know what their father did. When they were back, they came to realizethe problem: they usually arranged their books in their own orders, but thebooks seem to be in a great mess on the bookshelf now. They have to sort themright now!!

Each book has its own worth, which isrepresented by an integer. Books with odd values of worth belong to Alex andthe books with even values of worth belong to Bob. Alex has a habit of sortinghis books from the left to the right in an increasing order of worths, whileBob prefers to sort his books from the left to the right in a decreasing orderof worths.

At the same time, they do not want to changethe positions of the labels, so that after they have finished sorting the booksaccording their rules, each book's owner's name should match with the label inits position.

Here comes the problem. A sequenceof N values s0, s1, ..., sN-1 is given, whichindicates the worths of the books from the left to the right on the bookshelfcurrently. Please help the brothers to find out the sequence of worths aftersorting such that it satisfies the above description.

Input

The first line of input contains a singleinteger T, the number of test cases. Each test case starts with a linecontaining an integer N, the number of books on the bookshelf. The nextline contains N integers separated by spaces,representing s0, s1, ..., sN-1, which are the worths of thebooks.

Output

For each test case, output one linecontaining "Case #X: ", followed by t0, t1,..., tN-1 in order, and separated by spaces. X is the testcase number (starting from 1) and t0, t1, ...,tN-1 forms theresulting sequence of worths of the books from the left to the right.

Limits

1 ≤ T ≤ 30.

Small dataset

1 ≤ N ≤ 100
-100 ≤ si ≤ 100

Large dataset

1 ≤ N ≤ 1000
-1000 ≤ si ≤ 1000

Sample


Input


Output 
 

2

5

5 2 4 3 1

7

-5 -12 87 2 88 20 11

Case #1: 1 4 2 3 5

Case #2: -5 88 11 20 2 -12 87

此题可以抽象为,将输入的数重新排序,保证奇偶数位置不变,奇数按照升序排列,偶数按照降序排列。

int Alexcompare ( const void *a, const void *b )
{ 
	return *(int*)a - *(int*)b; 
}
int Bobcompare(const void *a, const void *b)
{
	return *(int*)b - *(int*)a;
}
void booksorting()
{
	 if (!fin.good())  
		 cout << "file is not prepared!" << endl;
	 int nCase;
	 fin>>nCase;
	 for (int i=0;i<nCase;i++)
	 {
		 int nBook;
		 fin>>nBook;
		 int nAlex(0),nBob(0);
		 int AlexBook[1000];
		 int BobBook[1000];
		 int nresult[1000];
		 int j(0);
		 for (;j<nBook;j++)
		 {
			 fin>>nresult[j];
			 if (nresult[j]%2==0)//Bob
			 {
				 BobBook[nBob]=nresult[j];
				 nBob++;
			 }
			 else
			 {
				 AlexBook[nAlex]=nresult[j];
				 nAlex++;
			 }
		 }
		 qsort(AlexBook,nAlex,sizeof(int),Alexcompare); 
		 qsort(BobBook,nBob,sizeof(int),Bobcompare); 
		 fout<< "Case #" <<i+1<< ": ";
		 int bobout(0),alexout(0);
		 for (int k=0;k<j;k++)
		 {
			 if (nresult[k]%2==0)
			 {
				 fout<<BobBook[bobout++]<<" ";
			 }
			 else
			 {
				 fout<<AlexBook[alexout++]<<" ";
			 }
		 }
	 }
	 fin.close();
	 fout.close();  
	 return ;
}

Problem D. Cross the maze

Problem

Edison, a robot, does not have a right handor eyes. As a brave robot, he always puts his left hand on the wall nomatter he walks or turns around. Because he thinks it is too dangerous, Edisondoes not walkbackward.

Assume that Edison has found himself in asquare-shaped maze of NxN square cellswhichis surrounded by walls from the outside. In the maze, some of the cellsare also walls. Edison can only move between two empty cells in fourdirections, north, south, west and east. In order to get out of the maze, hedrafts a plan.Heuses his left hand to lean on the wall and goes by following the wall.

Here is the question, is Edison able to getout of the maze in at most 10,000 steps? If he can make it, output the path. Bygetting out of the maze, he only needs to be in the exit cell. If the startingcell is the as the exit, Edison won't need to move and can directly get out ofthe maze.

Input

The first line of the input gives the numberof test cases, T. T test cases follow. Each test case startswith an integer N. N is the size of the maze. Thefollowing N lines, each line contains N characters whichmay be '.' or '#'.'.' is an empty cell, '#' is awall. Followed by a line which contains fourintegers: sx, sy, ex, ey. (sx, sy) means that Edisonis standing on row sx and column sy as his starting cell,(ex, ey) is the exit of the maze. (sx, sy) is guaranteed to be at oneof the 4 corners of the maze, and Edison can only touch the wall on 4 adjacentcells(not 8) initially. (ex, ey) can be anywhere in the maze. Note that thetop-left corner is at position (1,1).

Output

For each test case, output a line containing"Case #x: y", where x is the case number (starting from 1) and y is"Edison ran out of energy." (without the quotes) if Edison can'treach the exit of the maze in at most 10,000 steps, otherwise y should be thenumber of steps followed by another line which contains y characters todescribe the path (each character should beE for east, S for south, W for west or N for north). There is no character torepresent theturning around.We don't careabout the turning around steps, please only output the path of how Edison willcross the maze.

Limits

1 ≤ T ≤ 30.
1 ≤ sx, sy, ex, ey ≤ N.
The starting cell and the exit of the maze will always be an empty cell. Andthe starting cell and the exit of the maze won't be the same.

Small dataset

2 ≤ N ≤ 10.

Large dataset

2 ≤ N ≤ 100.

Sample


Input 
 


Output 
 

3

2

.#

#.

1 1 2 2

5

.##.#

.....

...#.

.###.

...#.

1 1 5 3

3

...

.#.

...

1 1 3 3

Case #1: Edison ran out of energy.

Case #2: 22

SEEENSESSSNNNWWSWWSSEE

Case #3: 4

EESS

Note: 
In the 2nd test case after moving 1 cell down from his starting cell, Edisonwill still be able to lean on the wall at the cell (1,2) by his lefthand. 
In the third test case, due to Edison can't touch the wall at cell (2,2)initially, so he has to go east in his first step.

迷宫问题,机器人位于N×N迷宫的某一角,而出口位于此外任意某点,机器人不会往回走,他每次都扶着墙走,而且因为只有左手,只能用左手扶着墙走,从而对其移动方向加以限制,按照迷宫策略,只要他沿着一边的墙一直走,不计转向,如果能能够找到出口,输出结果。如果在10000以内仍未走出,则认为其不能走出该迷宫。

BY springegg
 
const int dx[]={0,-1,0,1};
const int dy[]={1,0,-1,0};
const char ds[]={"ENWS"};
char g[110][110],ans[11000];
bool checkleft(int x,int y,int d){
	d=(d+1)%4;
	x+=dx[d];y+=dy[d];
	return g[x][y]=='#';
}
bool checkst(int x,int y,int d){
	x+=dx[d];y+=dy[d];
	return g[x][y]=='#';
}
void solve(){
	int n,i,j,sx,sy,tx,ty,d;
	fin>>n;
	for(i=1;i<=n;++i)
		fin>>(g[i]+1);
	for(i=0;i<=n+1;++i)
		g[i][0]=g[0][i]=g[n+1][i]=g[i][n+1]='#';
//surrounded by walls
	fin>>sx>>sy>>tx>>ty;
	if(sx==1 && sy==1){
		d=0;
	}else if(sx==1 && sy==n){
		d=3;
	}else if(sx==n && sy==n){
		d=2;
	}else {
		d=1;
	}
	if(sx==tx && sy==ty){
		fout<<0<<endl;
	}
	for(i=1;i<=10000;++i){
		if(!checkleft(sx,sy,d)){
			d=(d+1)%4;
			sx+=dx[d];sy+=dy[d];
			ans[i]=ds[d];
		}else {
			for(j=0;j<=3;++j){
				if(!checkst(sx,sy,d))break;
				d=(d+3)%4;
			}
			if(checkst(sx,sy,d))break;
			sx+=dx[d];sy+=dy[d];
			ans[i]=ds[d];
		}
		if(sx==tx && sy==ty){
			fout<<i<<endl;
			for(j=1;j<=i;++j) fout<<ans[j];
			fout<<endl;
			return;
		}
	}
	fout<<"Edison ran out of energy.\n";
}

void mazecross()
{
	int t,i;
	fin>>t;
	for(i=1;i<=t;++i){
		fout<<"Case #"<<i<<": ";
		solve();
	}
}
 

Problem E. Spaceship Defence

Problem

The enemy has invaded your spaceship, andonly superior tactics will allow you to defend it! To travel around your spaceship,your soldiers will use two devices: teleporters and turbolifts.

Teleporters allow your soldiers to moveinstantly between rooms. Every room contains a teleporter, and rooms arecolor-coded: if a soldier is in a room with some color, she can use the teleporterin that room to immediately move to any other room with the same color.

Turbolifts allow your soldiers to movebetween rooms more slowly. A turbolift is like an elevator that moves in manydirections. Each turbolift moves from one room to one other room, and it takesa certain amount of time to travel. Notes about turbolifts:

Turbolifts are not two-way: if a turbolift moves soldiers fromroom a to room b, the same turbolift cannot move soldiers fromroom b to room a, although there might be another turbolift thatdoes that.

More than one soldier can use the sameturbolift, and they do not interfere with each other in any way.

You will be given the locations anddestinations of several soldiers. For each soldier, output the minimum amountof time it could take that soldier to travel from his location to hisdestination.

Input

The first line of the input gives the numberof test cases, T. T test cases follow.

For every test case:

The first line of every test case containsan integer N, which is the number of rooms in your spaceship. The roomsare numbered from 1 to N. The following N lines each contain astring telling the color of the rooms, from room 1 to room N. The stringsonly contain characters a-z (the lower-case English letters)and 0-9 (the number 0 to 9), and the length of each string will beless than or equal to 2.

The next line in the test case is aninteger M, which indicates the number of turbolifts in your spaceship. Thefollowing M lines each contain 3 space-separatedintegers ai, bi, ti, telling us that there is a turbolift thatcan transport soldiers from room ai to room bi in ti seconds.

The next line in the test case contains aninteger S, which is the number of soldiers at your command. Thefollowing S lines each contain two integers: the location anddestination of one soldier, pj and qj.

Output

For each test case, output one line containingonly the string "Case #x:", where x is the number of the test case(starting from 1). On the next S lines, output a single integer: online j, the smallest number of seconds it could take for a soldier totravel from pj to qj. If there is no path from pj to qj,the integer you output should be -1.

Limits:

1 ≤ S ≤ 100.
1 ≤ ai, bi ≤ N.
0 ≤ ti ≤ 1000.
1 ≤ pj, qj ≤ N.

Small dataset:

1 ≤ T ≤ 10.
1 ≤ N ≤ 1000.
0 ≤ M ≤ 3000.

Large dataset:

T = 1.
1 ≤ N ≤ 80000.
0 ≤ M ≤ 3000.

Sample


Input 
 


Output 
 

3

3

gl

t3

t3

3

1 2 217

3 2 567

1 1 21

2

2 1

2 3

4

ca

bl

bl

8z

0

3

1 2

2 3

1 1

8

re

b7

ye

gr

0l

0l

ye

b7

7

4 1 19

2 4 21

2 5 317

4 5 34

4 7 3

4 8 265

8 6 71

3

4 3

2 6

1 4

Case #1:

-1

0

Case #2:

-1

0

0

Case #3:

3

55

-1

问题描述:有N个房间,每个房间都有对应的颜色标号(用小于等于两位的字母数字组合标志),相同颜色的房间可以利用Teleporters自由转换,另外还可以利用Turbolifts,每个Turbolifts有特定的使用方式,可以从A房间到B房间,花费T时间。然后给定若干人及其初始位置和目的位置,输出到达的最小时间,如不可达,输出-1。

此题可以抽象为N个点的加权有向图之间的最短路径问题,如果两个点同色则直接双向连通,权值为零。否则,若存在由A点到B点话费时间T的Turbolifts,则存在A到B的权重为T的边,由此构建图,即可转化为求两点之间的最短路径。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#include<vector>
#include<set>
#define maxn 2010
#define maxm 80100
#define maxl 1000000000
#define mod 1000003

using namespace std;
map<string,int> mm;
int tot;//map color to number
vector<pair<int,int> > v[maxn];
int d[maxn];

int find(const string &s){
	if(mm.count(s))return mm[s];
	mm[s]=++tot;
	v[tot].clear();
	return tot;
}

int dui[maxn];
bool visit[maxn];

int getDis(int x,int y){
	int head=0,tail=1,i,j,tmp;
	for(i=1;i<=tot;++i){
		d[i]=maxl;
		visit[i]=false;
	}
	d[x]=0;
	visit[x]=true;
	dui[1]=x;
	while(head!=tail){
		if(++head==maxn)head-=maxn;
		x=dui[head];
		for(i=0;i<v[x].size();++i){
			j=v[x][i].first;
			tmp=d[x]+v[x][i].second;
			if(tmp<d[j]){
				d[j]=tmp;
				if(!visit[j]){
					visit[j]=true;
				if(++tail==maxn)tail-=maxn;
					dui[tail]=j;
				}
			}
		}
		visit[x]=false;
	}
	if(d[y]<maxl)return d[y];else return -1;
}
string a[maxm];
void solve(){
	int n,i,m,x,y,z,q;
	cin>>n;
	mm.clear();
	tot=0;
	getline(cin,a[0]);
	for(i=1;i<=n;++i){
		getline(cin,a[i]);
	}//color
	cin>>m;
	for(i=1;i<=m;++i){
		cin>>x>>y>>z;//x->y in z
		x=find(a[x]);y=find(a[y]);
		v[x].push_back(make_pair(y,z));
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值