Google中国编程挑战赛第一轮

Google™ Code Jam 中国编程挑战赛第一轮2005年12月19日21时在线举行,3道难度不同的题目,分数各为250、500和1000,难度高的题目分数较高,编码时间75分钟。

Round 1 Problem 250 Statement

You want to buy two neighboring tickets 
in the first row of the theater so that one of the tickets is as far from the aisles as possible.
You will be given a String describing the first row of the theater where 
'.' represents an empty seat and 'X' represents an occupied seat. Your task is to return the index (from 0) of the empty seat that is furthest from the aisles (the two ends of the String) and is also next to an empty seat. If there are multiple possible seats, return the one with the smallest index. Return -1 if there are no seats that satisfy your requirements.
Definition

Class:
TheaterVisit
Method:
chooseSeat
Parameters:
string
Returns:
int
Method signature:
int chooseSeat(string row)
(be sure your method 
is public)

Constraints
-
row will contain between 
1 and 50 characters, inclusive.
-
Each character 
in row will be either '.' or 'X'.

Examples

0)

"....."
Returns: 
2
You can buy either tickets with indexes 
1 and 2 or tickets with indexes 2 and 3.

1)

"......"
Returns: 
2

2)

"..X..."
Returns: 
3
You should buy tickets with indexes 
3 and 4.

3)

".X.X..."
Returns: 
4

4)

"X.XX.X"
Returns: 
-1

5)

".."
Returns: 
0

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.

 

Round 1 Problem 500 Statement

A group of vertical blocks are placed densely one after another on the ground. The blocks each have a width of 
1, but their heights may vary. For example, if the heights of the vertical blocks are given as {1,5,5,1,6,1}, the configuration would look like the following picture:
    □
 □□ □
 □□ □
 □□ □
 □□ □
□□□□□□
Your task 
is to reproduce the exact shape of this structure using some number of non-intersecting rectangles. You will be given a int[] heights representing the heights of the vertical blocks from left to right. Return the minimal number of rectangles necessary to perform this task with the given configuration of blocks.

Definition

Class:
BlockStructure
Method:
cover
Parameters:
int[]
Returns:
int
Method signature:
int cover(int[] heights)
(be sure your method 
is public)

Constraints
-
heights will have between 
1 and 50 elements, inclusive.
-
Each element of heights will be between 
1 and 1000, inclusive.

Examples

0)

{
1,5,5,1,6,1}
Returns: 
3
    ◇
 □□ ◇
 □□ ◇
 □□ ◇
 □□ ◇
■■■■■■
We can use rectangles with sizes 6x1, 2x4 and 1x5.

1)

{
2,2,2,4,4}
Returns: 
2
   ■■
   ■■
□□□■■
□□□■■
We can use a 3x2 rectangle and a 2x4 rectangle.

2)

{
6,6,6,6,6,6}
Returns: 
1
The structure 
is a rectangle.

3)

{
71,44,95,16,10,80,12,17,98,61}
Returns: 
10
It
's impossible to use less than 10 rectangles.

4)

{
100,100,97,100,100,100,97,98,99,99}
Returns: 
5

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.

 

Round 1 Problem 1000 Statement

We have a sequence of integers, and we would like to remove all duplicate elements from 
this sequence. There may be multiple ways to perform this task. For example, given the sequence { 1213 }, we could end up with either { 123 } or { 213 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence. For this problem, we want to return the lexicographically first of of all possible remaining sequences. A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ (so, for example, { 123 } comes before { 231 }).
You will be given a 
int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.

Definition

Class:
HardDuplicateRemover
Method:
process
Parameters:
int[]
Returns:
int[]
Method signature:
int[] process(int[] sequence)
(be sure your method 
is public)

Constraints
-
sequence will have between 
1 and 50 elements, inclusive.
-
Each element of sequence will be between 
1 and 1000, inclusive.

Examples

0)

{
565165}
Returns: {
165 }
There are six different ways to remove duplicates (remaining numbers are marked by 
'*'):
*5*6,  5*1,  6,  5},
*5,  6,  5*1*6,  5},
{  
5*6*5*1,  6,  5},
{  
5,  6*5*1*6,  5},
{  
5*6,  5*1,  6*5},
{  
5,  6,  5*1*6*5}.

The last variant 
is the lexicographically first.

1)

{
324244}
Returns: {
324 }

2)

{
666666}
Returns: {
6 }

3)

{
132423}
Returns: {
1243 }

4)

{
5415}
Returns: {
415 }

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.

posted on 2005-12-20 11:03 空间/IV 阅读(16) 评论(3)  编辑 收藏 收藏至365Key 所属分类: 算法C# Base

评论

# tangxin的C#解答 2005-12-20 15:27 空间/IV

 1public class TheaterVisit
 2{
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1public class BlockStructure
 2{
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1using System.Collections;
 2
 3public class HardDuplicateRemover
 4{
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}

 1public class TheaterVisit
 2{
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1public class BlockStructure
 2{
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1using System.Collections;
 2
 3public class HardDuplicateRemover
 4{
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}
  

# hyyylr的C#解答 2005-12-20 15:29 空间/IV

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}
  

# az的C#解答 2005-12-20 16:28 空间/IV

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}
# tangxin的C#解答 2005-12-20 15:27 空间/IV

 1public class TheaterVisit
 2{
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1public class BlockStructure
 2{
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1using System.Collections;
 2
 3public class HardDuplicateRemover
 4{
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}

 1public class TheaterVisit
 2{
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1public class BlockStructure
 2{
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1using System.Collections;
 2
 3public class HardDuplicateRemover
 4{
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}
  

# hyyylr的C#解答 2005-12-20 15:29 空间/IV

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}
  

# az的C#解答 2005-12-20 16:28 空间/IV

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}

 1public class BlockStructure
 2{
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}

google中国编程挑战赛-练习题之二(500分题)

12-08

继续是E文~哭~~rnrnProblem Statementrn rnA square matrix is a grid of NxN numbers. For example, the following is a 3x3 matrix:rn 4 3 5rn 2 4 5rn 0 1 9rnOne way to represent a matrix of numbers, each of which is between 0 and 9 inclusive, is as a row-major String. To generate the String, simply concatenate all of the elements from the first row followed by the second row and so on, without any spaces. For example, the above matrix would be represented as "435245019". You will be given a square matrix as a row-major String. Your task is to convert it into a String[], where each element represents one row of the original matrix. Element i of the String[] represents row i of the matrix. You should not include any spaces in your return. Hence, for the above String, you would return "435","245","019". If the input does not represent a square matrix because the number of characters is not a perfect square, return an empty String[], .rnDefinitionrn rnClass:rnMatrixToolrnMethod:rnconvertrnParameters:rnStringrnReturns:rnString[]rnMethod signature:rnString[] convert(String s)rn(be sure your method is public)rn rnrnConstraintsrn-rns will contain between 1 and 50 digits, inclusive.rnExamplesrn0)rnrn rn"435245019"rnReturns: "435", "245", "019" rnThe example above.rn1)rnrn rn"9"rnReturns: "9" rnrn2)rnrn rn"0123456789"rnReturns: rnThis input has 10 digits, and 10 is not a perfect square.rn3)rnrn rn"3357002966366183191503444273807479559869883303524"rnReturns: "3357002", "9663661", "8319150", "3444273", "8074795", "5986988", "3303524" rnrnThis 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.

google中国编程挑战赛-练习题之三(250分题)

12-08

哎,看来我得先去学几年E再说了~TTrnrnrnProblem Statementrn rnA simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. The upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19). You are given a String[], commands, each element of which contains one of two possible commands. A command of the form "FORWARD x" means that the cursor should move forward by x pixels. Each pixel on its path, including the start and end points, is painted black. The only other command is "LEFT", which means that the cursor should change its direction by 90 degrees counterclockwise. So, if the cursor is initially pointing straight down and it receives a single "LEFT" command, it will end up pointing straight to the right. Execute all the commands in order and return the resulting 20 x 20 pixel canvas as a String[] where character j of element i represents the pixel at (i, j). Black pixels should be represented as uppercase 'X' characters and blank pixels should be represented as '.' characters.rnDefinitionrn rnClass:rnDrawLinesrnMethod:rnexecuternParameters:rnString[]rnReturns:rnString[]rnMethod signature:rnString[] execute(String[] commands)rn(be sure your method is public)rn rnrnNotesrn-rnThe cursor only paints the canvas if it moves (see example 1).rnConstraintsrn-rncommands will contain between 1 and 50 elements, inclusive.rn-rnEach element of commands will be formatted as either "LEFT" or "FORWARD x" (quotes for clarity only), where x is an integer between 1 and 19, inclusive, with no extra leading zeros.rn-rnWhen executing the commands in order, the cursor will never leave the 20 x 20 pixel canvas.rnExamplesrn0)rnrn rn"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"rnReturns: rn"XXXXXXXXXXXXXXXXXXXX",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "X..................X",rn "XXXXXXXXXXXXXXXXXXXX" rnThis sequence of commands draws a 20 x 20 outline of a square. The cursor is initially at (0, 0) pointing straight down. It then travels to (0, 19) after the first FORWARD command, painting each pixel along its path with a '*'. It then rotates 90 degrees left, travels to (19, 19), rotates 90 degrees left, travels to (19, 0), rotates 90 degrees left, and finally travels back to (0, 0).rn1)rnrn rn"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"rnReturns: rn"....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "...................." rnThe cursor spins round and round, but never actually paints any pixels. The result is an empty canvas.rn2)rnrn rn"FORWARD 1"rnReturns: rn"X...................",rn "X...................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "....................",rn "...................." rnGoing forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted.rn3)rnrn rn"LEFT", "FORWARD 19", "LEFT", "LEFT", "LEFT",rn "FORWARD 18", "LEFT", "LEFT", "LEFT", "FORWARD 17",rn "LEFT", "LEFT", "LEFT", "FORWARD 16", "LEFT",rn "LEFT", "LEFT", "FORWARD 15", "LEFT", "LEFT", "LEFT",rn "FORWARD 14", "LEFT", "LEFT", "LEFT", "FORWARD 13",rn "LEFT", "LEFT", "LEFT", "FORWARD 12", "LEFT", "LEFT",rn "LEFT", "FORWARD 11", "LEFT", "LEFT", "LEFT", "FORWARD 10",rn "LEFT", "LEFT", "LEFT", "FORWARD 9", "LEFT", "LEFT",rn "LEFT", "FORWARD 8", "LEFT", "LEFT", "LEFT", "FORWARD 7"rnReturns: rn"XXXXXXXXXXXXXXXXXXXX",rn "...................X",rn "..XXXXXXXXXXXXXXXX.X",rn "..X..............X.X",rn "..X.XXXXXXXXXXXX.X.X",rn "..X.X..........X.X.X",rn "..X.X.XXXXXXXX.X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.X........X.X.X",rn "..X.X.XXXXXXXXXX.X.X",rn "..X.X............X.X",rn "..X.XXXXXXXXXXXXXX.X",rn "..X................X",rn "..XXXXXXXXXXXXXXXXXX",rn "...................." rnrnThis 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.

google中国编程挑战赛-练习题之一(1000分题)

12-08

全英文滴~看不太懂啊~~~~55555555555555rnrnProblem Statementrn rnWhen editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line. You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a string where characters of the string represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.rnrnDefinitionrn rnClass:rnCursorPositionrnMethod:rngetPositionrnParameters:rnstring, intrnReturns:rnintrnMethod signature:rnint getPosition(string keystrokes, int N)rn(be sure your method is public)rn rnrnConstraintsrn-rnkeystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.rn-rnN will be between 1 and 100, inclusive.rnrnExamplesrn0)rnrn rn"ERLLL"rn10rnReturns: 7rnFirst, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.rn1)rnrn rn"EHHEEHLLLLRRRRRR"rn2rnReturns: 2rnAll the right-arrows at the end ensure that we end up at the end of the line.rn2)rnrn rn"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"rn10rnReturns: 3rnrn3)rnrn rn"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"rn19rnReturns: 12rnrnThis 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.

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭