惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
AI
AI
Webroot Blog
Webroot Blog
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
B
Blog RSS Feed
小众软件
小众软件
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Vercel News
Vercel News
Y
Y Combinator Blog
P
Proofpoint News Feed
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
MongoDB | Blog
MongoDB | Blog
SecWiki News
SecWiki News
The Register - Security
The Register - Security
博客园_首页
T
Threat Research - Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
I
InfoQ
雷峰网
雷峰网
C
Check Point Blog

博客园 - 空军

过多边形边上某点的任意直线等分面积 VisualAPL Installer for Visual Studio 2008 [ZT] Create a Microsoft Access Database Using ADOX and Visual Basic .NET 数的分解,据说是清华的一道复试上机题 为System.Windows.Forms.FontDialog类添加Location属性 f(f(x)) = -x [zt]〖Math〗构造函数使得任意小的区间所对应的值域都是整个实数域 [zt]鸟巢、水立方:同一个地方,同一梦想 〖Math〗据传,任意锐角等于0° 爱因斯坦的超级问题(谁养鱼)SQL解法 “槑囧圐圙”您认得这些汉字吗? 根据URL提取页面的Title,根据网页的charset自动判断Encoding MSDN“MidpointRounding 枚举”中文翻译有误 C# 2.0 新特性(泛型、可空类型)应用一例 表达式计算器 Google速记 Google中国编程挑战赛第一轮 数学家 数据库访问模块
Google中国编程挑战赛资格赛
空军 · 2005-12-15 · via 博客园 - 空军

Google™ Code Jam 中国编程挑战赛资格赛2005年12月12日中午12时-13日中午12时在线举行,参赛者会抽到5组题中的1组,每组题有2道难度不同的题目,分数各为250和750,难度高的题目分数较高。参赛者有60分钟可以完成全部的2题或其中1题。我抽到的是第3组题,以237.97分的成绩晋级下一轮比赛,在入围的500名参赛者中排名第189位。

Qualification Groups 3/7/15/17/23 Problem 250 Statement

You have several identical balls that you wish to place 

in several baskets. Each basket has the same maximum capacity. You are given an int baskets, the number of baskets you have. You are given an int capacity, the maximum capacity of each basket. Finally you are given an int balls, the number of balls to sort into baskets. Return the number of ways you can divide the balls into baskets. If this cannot be done without exceeding the capacity of the baskets, return 0.
Each basket 
is distinct, but all balls are identical. Thus, if you have two balls to place into two baskets, you could have (02), (11), or (20), so there would be three ways to do this.

Definition

Class:
FillBaskets
Method:
countWays
Parameters:

intintint
Returns:
int
Method signature:
int countWays(int baskets, int capacity, int balls)
(be sure your method 
is public)

Constraints

-
baskets will be between 
1 and 5, inclusive.
-
capacity will be between 
1 and 20, inclusive.
-
balls will be between 
1 and 100, inclusive.

Examples

0)2
20
2
Returns: 
3
The example from the problem statement.
1)3
20
1
Returns: 
3
We have only 
1 ball, so we must choose which of the three baskets to place it in.2)3
20
2
Returns: 
6
We can place both balls 
in the same basket (3 ways to do this), or one ball in each of two baskets (3 ways to do this).3)1
5
10
Returns: 
0
We have more balls than our basket can hold.
4)4
5
10
Returns: 
146

This problem statement 

is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 1 public class FillBaskets
 2 {
 3   public int countWays(int baskets, int capacity, int balls)
 4   {
 5     if (baskets * capacity < balls) return 0;
 6     if (baskets == 1return 1;
 7     int n = 0;
 8     int b = (balls < capacity ? balls : capacity);
 9     for (int i = 0; i <= b; i++)
10     {
11       n += countWays(baskets - 1, capacity, balls - i);
12     }
13     return n;
14   }
15 }

 250分的题比较简单,使用递归可以了。

Qualification Groups 3/7/15/17/23 Problem 750 Statement

You are given a 

string[] grid representing a rectangular grid of letters. You are also given a string find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
You are to 
return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000return -1.

Definition

Class:
WordPath
Method:
countPaths
Parameters:

string[], string
Returns:
int
Method signature:
int countPaths(string[] grid, string find)
(be sure your method 
is public)

Constraints

-
grid will contain between 
1 and 50 elements, inclusive.
-
Each element of grid will contain between 
1 and 50 uppercase ('A'-'Z') letters, inclusive.
-
Each element of grid will contain the same number of characters.
-
find will contain between 
1 and 50 uppercase ('A'-'Z') letters, inclusive.

Examples

0)

{

"ABC",
 
"FED",
 
"GHI"}
"ABCDEFGHI"
Returns: 
1
There 
is only one way to trace this path. Each letter is used exactly once.1)

{

"ABC",
 
"FED",
 
"GAI"}
"ABCDEA"
Returns: 
2
Once we 
get to the 'E', we can choose one of two directions for the final 'A'.2)

{

"ABC",
 
"DEF",
 
"GHI"}
"ABCD"
Returns: 
0
We can trace a path 
for "ABC", but there's no way to complete a path to the letter 'D'.

3)

{

"AA",
 
"AA"}
"AAAA"
Returns: 
108
We can start from any of the four locations. From each location, we can then move 
in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.4)

{

"ABABA",
 
"BABAB",
 
"ABABA",
 
"BABAB",
 
"ABABA"}
"ABABABBA"
Returns: 
56448
There are a lot of ways to trace 
this path.5)

{

"AAAAA",
 
"AAAAA",
 
"AAAAA",
 
"AAAAA",
 
"AAAAA"}
"AAAAAAAAAAA"
Returns: 
-1
There are well over 
1,000,000,000 paths that can be traced.6)

{

"AB",
 
"CD"}
"AA"
Returns: 
0
Since we can
't stay on the same cell, we can't trace the path at all.

This problem statement 

is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 1 public class WordPath
 2 {
 3   const int MAX = 1000000000;
 4   int[,] g;
 5   int[]  f;
 6   int    flen;
 7   
 8   int paths(int i0, int j0, int k)
 9   {
10     if (k == flen) return 1;
11     int n = 0;
12     for (int i = i0 - 1; i <= i0 + 1; i++)
13     {
14       for (int j = j0 - 1; j <= j0 + 1; j++)
15       {
16         if (i == i0 && j == j0) continue;
17         if (f[k] == g[i, j])
18         {
19           n += paths(i, j, k+1);
20           if (n > MAX) return MAX + 1;
21         }
22       }
23     }
24     return n;
25   }
26   
27   public int countPaths(string[] grid, string find)
28   {
29     flen = find.Length;
30     f    = new int[flen];
31     for (int i = 0; i < flen; i++)
32     {
33       f[i] = find[i];
34     }
35     int rows = grid.Length;
36     int cols = grid[0].Length;
37     g = new int[rows+2, cols+2];
38     for (int i = 0; i < rows; i++)
39     {
40       for (int j = 0; j < cols; j++)
41       {
42         g[i+1, j+1= grid[i][j];
43       }
44     }
45     int n = 0;
46     for (int i = 1; i <= rows; i++)
47     {
48       for (int j = 1; j <= cols; j++)
49       {
50         if (f[0== g[i, j])
51         {
52           n += paths(i, j, 1);
53           if (n > MAX) return -1;
54         }
55       }
56     }
57     return n;
58   }
59 }

750分的题我也使用了递归,结果对 Example 5 的输入计算时间超过2秒而在系统测试阶段被判为0分。资格赛结束后看了 happyyellow 的解答:

 1 using System.Drawing;
 2 using System.Collections;
 3 
 4 public class WordPath
 5 {
 6   public int countPaths(string[] grid, string find)
 7   {
 8     ArrayList[] lists = new ArrayList[26];
 9     for (int i = 0; i < lists.Length; i++)
10     {
11       lists[i] = new ArrayList();
12     }
13     
14     int il = grid.Length;
15     int jl = grid[0].Length;
16     for (int i = 0; i < il; i++)
17     {
18       for (int j = 0; j < jl; j++)
19       {
20         lists[grid[i][j] - 'A'].Add(new Point(i+1, j+1));
21       }
22     }
23     
24     long[,] m = new long[il+2, jl+2];
25     foreach (Point p in lists[find[0- 'A'])
26     {
27       m[p.X, p.Y] = 1;
28     }
29     for (int d = 1; d < find.Length; d++)
30     {
31       long[,] mm = new long[il+2, jl+2];
32       foreach (Point p in lists[find[d] - 'A'])
33       {
34         mm[p.X, p.Y] =
35           m[p.X-1, p.Y+1+ m[p.X, p.Y+1+ m[p.X+1, p.Y+1+
36           m[p.X-1, p.Y  ] +                 m[p.X+1, p.Y  ] +
37           m[p.X-1, p.Y-1+ m[p.X, p.Y-1+ m[p.X+1, p.Y-1];
38       }
39       m = mm;
40     }
41     long n = 0;
42     for (int i = 1; i <= il; i++)
43     {
44       for (int j = 1; j <= jl; j++)
45       {
46         n += m[i, j];
47       }
48     }
49     if (n > 1000000000return -1;
50     return (int)n;
51   }
52 }