Codeforces979B、Treasure Hunt(模拟)


B. Treasure Hunt

time limit per test

1 second

memory limit per test

256 megabytes


standard input


standard output

After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.

The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.

A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of 77 because its subribbon a appears 77 times, and the ribbon abcdabc has the beauty of 22 because its subribbon abc appears twice.

The rules are simple. The game will have nn turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after nn turns wins the treasure.

Could you find out who is going to be the winner if they all play optimally?


The first line contains an integer nn (0≤n≤1090≤n≤109) — the number of turns.

Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than 105105 uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.


Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".



























In the first example, after 33 turns, Kuro can change his ribbon into ooooo, which has the beauty of 55, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most 44, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.

In the fourth example, since the length of each of the string is 99 and the number of turn is 1515, everyone can change their ribbons in some way to reach the maximal beauty of 99 by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.















using namespace std;
const int inf=0x3f3f3f3f;

int n;
int len;
int max1,max2,max3;
void print()
	else if(max2>max1&&max2>max3)
	else if(max3>max1&&max3>max2)
int work()
	char s[200005];
	int cnt[300];
	int ans=-1;
	int len=strlen(s);
	for(int i=0;i<len;i++)
		return len-1;
	else if(len-ans>=n)
		return ans+n;
		return len;
int main()



Treasure Hunt


DescriptionnnArcheologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors. nAn example is shown below: n![]( input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).nOutputnnPrint a single line listing the minimum number of doors which need to be created, in the format shown below.nSample Inputnn7 n20 0 37 100 n40 0 76 100 n85 0 0 75 n100 90 0 90 n0 71 100 61 n0 14 100 38 n100 47 47 100 n54.5 55.4 nSample OutputnnNumber of doors = 2

Google Treasure Hunt 2008 素数


题目:连续 5 个素数之和rn 连续 13 个素数之和rn 连续 275 个素数之和rn 连续 1187 个素数之和rn 求出同时符合以上4个条件的数。rnrn技巧:用数组里的一个位保存一个数是否是素数。rn结果:5615741rn计算用时:17秒 (AMD 2600+)rnrn[code=Delphi(Pascal)]rnunit GoogleHunt5u;rnrninterfacernrnusesrn Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,rn StdCtrls, Spin, SearchDir;rnrntypern TForm1 = class(TForm)rn Button1: TButton;rn Edit1: TEdit;rn Label3: TLabel;rn Label2: TLabel;rn Edit3: TEdit;rn procedure Button1Click(Sender: TObject);rn procedure FormCreate(Sender: TObject);rn privatern Private declarations rn publicrn Public declarations rn end;rnrnvarrn Form1: TForm1;rnrnfunction IsPrime(V: DWORD): Boolean;rnrnimplementationrnrn$R *.DFMrnrnvarrn bitArr: array [0..HIGH(DWORD) div 32] of DWORD;rnrn InitMax: DWORD = 3;rnrnfunction IsPrimeFromBit(V: DWORD): Boolean;rnvarrn p: integer; // 下标rn m: integer; // 余rn mask: DWORD;rnbeginrn if V = DWORD(-1) thenrn sleep(0); // ERRORrn while InitMax < V dorn beginrn if IsPrime(InitMax + 2) thenrn beginrn Inc(InitMax, 2);rn p := InitMax shr 5;rn m := InitMax and $1F;rn mask := 1 shl m;rn bitArr[p] := bitArr[p] or mask;rn endrn elsern Inc(InitMax, 2);rn end;rn p := V shr 5;rn m := V and $1F;rn if m = 0 thenrn beginrn Result := False;rn Exit;rn end;rn mask := 1 shl m;rn if p < 0 thenrn Sleep(1);rn Result := bitArr[p] and mask <> 0;rnend;rnrnfunction IsPrime(V: DWORD): Boolean;rnvarrn i: integer;rn max: DWORD;rnbeginrn max := Round(Sqrt(V));rn i := 3;rn while i <= max dorn beginrn if IsPrimeFromBit(i) thenrn if V mod i = 0 thenrn beginrn Result := False;rn Exit;rn end;rn Inc(i);rn end;rn Result := True;rnend;rnrnfunction NprePrime(V: DWORD; n: integer): integer;rnbeginrn if IsPrimeFromBit(V) thenrn Dec(n, 2)rn elsern Dec(n);rn Dec(V);rn while n > 0 dorn beginrn if IsPrimeFromBit(V) thenrn beginrn Dec(n);rn if n = 0 thenrn beginrn Result := V;rn Exit;rn end;rn end;rn Dec(V);rn if V = 2 thenrn beginrn Result := V;rn Exit;rn endrn end;rnend;rnrnfunction GetNextPrime(V: DWORD): DWORD;rnbeginrn Inc(V);rn while not IsPrimeFromBit(V) dorn beginrn Inc(V);rn end;rn Result := V;rnend;rnrnfunction SumNPrime(V: DWORD; n: integer): DWORD;rnbeginrn Result := V;rn Dec(n);rn while n > 0 dorn beginrn V := GetNextPrime(V);rn Inc(Result, V);rn Dec(n);rn end;rnend;rnrnfunction IsNPrimeSum(V: DWORD; n: integer): Boolean;rnvarrn stV: DWORD;rn sumN: DWORD;rnbeginrn stV:= NprePrime((V div n), n);rn repeatrn sumN := SumNPrime(stV, n);rn stV := GetNextPrime(stV);rn until sumN >= V;rn Result := sumN = V;rnend;rnrnprocedure TForm1.Button1Click(Sender: TObject);rnlabel repgo;rnconstrn Cnt1 = 5;rn Cnt2 = 13;rn Cnt3 = 275;rn Cnt4 = 1187;rnvarrn stV: DWORD;rn st: DWORD;rnbeginrn st := GetTickCount();rn stV := SumNPrime(2, Cnt4);rn while True dorn beginrn Edit1.Text := 'Calc... ' + IntToStr(stV);rn Application.ProcessMessages();rn if IsNPrimeSum(stV, Cnt1) thenrn if IsNPrimeSum(stV, Cnt2) thenrn if IsNPrimeSum(stV, Cnt3) thenrn if IsNPrimeSum(stV, Cnt4) thenrn beginrn Edit1.Text := IntToStr(stV);rn Break;rn end;rn if stV and 1 = 0 then // 偶数rn Inc(stV)rn elsern Inc(stV, 2);rn end;rn st := GetTickCount() - st;rn Edit3.Text := IntToStr(st div 1000);rnend;rnrnprocedure TForm1.FormCreate(Sender: TObject);rnbeginrn FillChar(bitArr, sizeof(bitArr), 0);rn bitArr[0] := $C; // 2,3 are Primernend;rnrnend.rn[/code]

Google Treasure Hunt 2008 走格式题解


题目: 34x68的格子,从左上角到右下角,有多少种走法。规则:只能向右或向下行走。rn解题方法: 从一个格子到右下角的走法数量,等于它右边格子与下边格子的走法数量之和,从最下边的格子或最右边的格子到右下角只有一种走法。rn结果: 0294692427022540894366527900rn计算用时: 瞬间rn注: 用String做大数计算。rnrn[code=Delphi(Pascal)]rnunit GoogleHunt2u;rnrninterfacernrnusesrn Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,rn StdCtrls, Spin;rnrntypern TForm1 = class(TForm)rn SpinEdit1: TSpinEdit;rn Label1: TLabel;rn Label2: TLabel;rn SpinEdit2: TSpinEdit;rn Button1: TButton;rn Edit1: TEdit;rn Label3: TLabel;rn procedure Button1Click(Sender: TObject);rn procedure FormCreate(Sender: TObject);rn privatern Private declarations rn publicrn Public declarations rn end;rnrnvarrn Form1: TForm1;rnrnimplementationrnrn$R *.DFMrnrnvarrn VArr: array [0..100, 0..100] of string;rnrnfunction BigIntAdd(V1, V2: string): string;rnvarrn i: integer;rn max1, max2: integer;rn max: integer;rn v: integer;rnbeginrn if V1[1] = '0' thenrn max1 := Length(V1) - 1rn elsern max1 := Length(V1);rn if V2[1] = '0' thenrn max2 := Length(V2) - 1rn elsern max2 := Length(V2);rn if max1 > max2 thenrn max := max1 + 1rn elsern max := max2 + 1;rnrn while Length(V1) < max dorn beginrn V1 := '0' + V1;rn end;rn while Length(V2) < max dorn beginrn V2 := '0' + V2;rn end;rnrn SetLength(Result, max);rn v := 0;rn for i := max downto 1 dorn beginrn v := Byte(V1[i]) + Byte(V2[i])-Byte('0')*2 + v;rn Result[i] := Char((v mod 10) + Byte('0'));rn v := v div 10;rn end;rnend;rnrnfunction CountRoute(W, H: integer): string;rnbeginrn if (W = 1) or (H = 1) thenrn Result := '1'rn elsern beginrn if VArr[W-1][H] = '' thenrn beginrn VArr[W-1][H] := CountRoute(W-1, H);rn VArr[H][W-1] := VArr[W-1][H];rn end;rn if VArr[W][H-1] = '' thenrn beginrn VArr[W][H-1] := CountRoute(W, H-1);rn VArr[H-1][W] := VArr[W][H-1];rn end;rn Result := BigIntAdd(VArr[W-1][H], VArr[W][H-1]);rn end;rnend;rnrnprocedure TForm1.Button1Click(Sender: TObject);rnvarrn W, H: integer;rnbeginrn W := SpinEdit1.Value;rn H := SpinEdit2.Value;rn Edit1.Text := CountRoute(W, H);rnend;rnrnprocedure TForm1.FormCreate(Sender: TObject);rnbeginrn FillChar(VArr, sizeof(VArr), 0);rnend;rnrnend.rn[/code]rn

Google Treasure Hunt 2008 zip 遍历找文件程序(字符匹配)


谷歌猎宝2008 rn问题名:"zip"rn这里有一个随机 .zip 文件给你下载:rnrn xxxxxxxxxxxxxxxxxxxxxxxxx.ziprnrn解压缩这个文档,然后处理里面的文件,来获得一个数字结果。rn你将要做的是按照要求的描述,提取指定匹配文件的里面的行,然后把它们都加起来,最后把几个这样的和数,再乘起来得到最终的结果。 rn注意文件有许多扩展名,像".pdf" .js,但是所有都是纯文本文件,都包含几行的文本。rnrn要求描述如下:rn找到所有,不管文件名还是它的路径里面,有几个字是 "BCD"的文件,后缀名是 .pdf 的文件,把里面的第四行的数都加起来;rn找到所有,不管文件名还是它的路径里面,有几个字是 "abc"的文件,后缀名是 .pdf 的文件,把里面的第四行的数都加起来;rnHint: If the requested line does not exist, do not increment the sum. rn提示:如果遇到没有(第四)行的,就不加。rnrn把上面所有的的"和"乘起来,然后在下面输入那个 "积"rn(注意:答案必须是一个准确的十进制表达的数)rnrnrn原文:rnGoogle Treasure Hunt 2008rnrnQuestion ziprnHere is a random zip archive for you to download:rnrn rnrnUnzip the archive, then process the resulting files to obtain a numeric result. rnYou'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. rnNote that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text. rnrnSum of line 4 for all files with path or name containing BCD and ending in .pdfrnSum of line 4 for all files with path or name containing abc and ending in .pdfrnHint: If the requested line does not exist, do not increment the sum. rnrnMultiply all the above sums together and enter the product below.rn(Note: Answer must be an exact, decimal representation of the number.) rnrnYour answer:rnrnrn rnrn rnrn©2008 Google - Terms of Service - Privacy Policy - Google Home rnPowered by

The Treasure


We have arrived at the age of the Internet. Many software applications have transformed from stand-alone to online applications. Computer games are following this trend as well. Online games are becoming more and more popular, not only because they are more intelligent, but also because they can bring great profits. "The computer game industry is developing rapidly in China. Online game revenues amounted to 1.3 billion Yuan last year and are expected to reach 6.7 billion Yuan by 2007." reported by China Daily in 2004.nnHowever, good games originate from good programmers. We take for example that there is a RPG (Role Playing Game) and your boss asks you to implement some tasks. For simplicity's sake, we assume there are two kinds of roles in this game: one is player and the other is monster. You should help the player to achieve the goal: reach the place where treasure is positioned as early as possible and get the treasure.nnThe map of the game is a matrix of N * M identical cells. Some cells are passable blocks, and others are non-passable rocks. At any time, there is at most one role occupying a block. At the beginning, the time is set to 0, and the player is at a certain block. He then moves towards the treasure. At each turn, we have some rules:nnThe player can stay in the same block during the next one-second time duration, or he can walk or run towards the east, south, west, north, northeast, northwest, southeast, and southwest.nnnWith walking, the player can arrive at the corresponding passable blocks around him (See Fig.1). Each move takes 1 second.nWith running, the player can arrive at the corresponding passable blocks 2 cells away from him (See Fig.2). Each run takes 1 second. As demonstrated in Fig.3, if a neighbor cell is not passable, the player cannot run in that direction. For example, if cell 2 is a rock, running from 1 to 3 is impossible.nThe monsters are classified into aggressive and non-aggressive. If a monster occupies a cell, the player cannot move into that cell or run through that cell. In addition, the player cannot move into the cells surrounding an aggressive monster, because it will attack the player near it. For example, in Fig.4, if there is an aggressive monster in 5, then the cell 1, 2, 3, 4, 6, 7, 8 and 9 are in its attacking region, so the player cannot stay in or pass through these cells.nMonsters change their positions each turn. Each monster appears by its position sequence iteratively. That's to say, given the position sequence of monster i: (x1, y1), (x2, y2), ..., (xs, ys), its initial position is (x1, y1) at time 0, then it appears in (x2, y2) at time 1, and so on. When monster i arrives at (xs, ys) at time s-1, it will arrive in (x1, y1) at time s, and start to repeat.nAt the start of each turn, all the monsters change their positions first (the way of changing is given above). If a monster appears in the player's cell, or if an aggressive monster appears near the player to put him in its attacking region, the player will die, and the goal cannot be achieved. After all the monsters change their positions, the player makes a move or stays in the same cell. In his move, the moving path should not be occupied by any rocks or monsters or in the attacking region of any aggressive monsters. When counting the total time, we can neglect the time between monsters' position change and the player's move. Given the map of the game, the player's starting position, the treasure position and all the monsters' positions in every second, your task is to write a program to find the minimum time that the player gets the treasure.nInputnnThe input consists of several test cases. The first line of each case contains two integers N and M (1 <= N, M <= 100), where N is the height of the map and M is the width of the map. This is followed by N lines each containing M characters representing the map. A '#' represents a rock, a '.' is a free block, 'p' is the starting position of the player, 't' is the position of the treasure, 'n' is the initial position of a non-aggressive monster, and an 'a' stands for the initial position of an aggressive monster.nnThe cell (i, j) is the j-th cell on the i-th row counting from left to right. The rows are counted from 1 to N starting from the first line of the matrix. We can number all the monsters as 1, 2, 3�� according to their initial position, sorting first by row, then by column. The (n+2)-th line contains an integer p (0 <= p <= 100), which is the total number of monsters (i.e. the total number of 'n's and 'a's in the matrix). It is followed by p lines each specifying a monster's position sequence in the following format: the i-th (1 <= i <= p) line corresponds to monster i, which begins with an integer s (1 <= s <= 100), meaning the length of position sequence. Then s pairs of integers x1, y1, x2, y2, ��, xs, ys are followed, separated by blanks. Each pair is a free block in the map, (i.e. a monster never goes to a rock cell).nnIt is assured that none of the aggressive monsters' initial position is around the player. Two consecutive cases are separated by a blank line. The input is terminated by a line containing a pair of zeros.nnOutputnnFor each test case, output the minimum total time required for the player to get the treasure, in seconds. If it's not possible to get the treasure, or the minimum required time is greater than 100 seconds, please print a line just containing the string "impossible". Two consecutive cases should be separated by a blank line.nnSample Inputnn7 8n#.#####.n#.t#..p.n#..#....n..#a.#.#n#...##.nn.#......n........n2n2 4 4 5 4n3 5 8 6 8 5 7nn3 3np#.n##.nt..n0nn2 2n#tnp#n0nn0 0nnSample Outputnn8nnimpossiblenn1