关于Google挑战赛

 

模拟题1

Problem Statement
    
When 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.
Definition
    
Class:
CursorPosition
Method:
getPosition
Parameters:
String, int
Returns:
int
Method signature:
int getPosition(String keystrokes, int N)
(be sure your method is public)
    

Constraints
-
keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
-
N will be between 1 and 100, inclusive.
Examples
0)

    
"ERLLL"
10
Returns: 7
First, 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.
1)

    
"EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line.
2)

    
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3

3)

    
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12

代码:

public class CursorPosition

{

    public int getPosition(string keystrokes, int N)

    {

        if (keystrokes == null || keystrokes == "")

            return N;

 

        int nRet = N;

 

        for (int i = 0; i < keystrokes.Length; i++)

        {

            switch (keystrokes[i])

            {

                case 'L':

                    nRet--;

                    break;

                case 'R':

                    nRet++;

                    break;

                case 'H':

                    nRet = 0;

                    break;

                case 'E':

                    nRet = N;

                    break;

                default:

                    continue;

 

            }

 

            if (nRet < 0) nRet = 0;

            if (nRet > N) nRet = N;

        }

 

        return nRet;

    }

}

模拟题2

Problem Statement
    
A 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.
Definition
    
Class:
DrawLines
Method:
execute
Parameters:
string[]
Returns:
string[]
Method signature:
string[] execute(string[] commands)
(be sure your method is public)
    

Notes
-
The cursor only paints the canvas if it moves (see example 1).
Constraints
-
commands will contain between 1 and 50 elements, inclusive.
-
Each 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.
-
When executing the commands in order, the cursor will never leave the 20 x 20 pixel canvas.
Examples
0)

    
{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"}
Returns:
{"XXXXXXXXXXXXXXXXXXXX",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "XXXXXXXXXXXXXXXXXXXX" }
This 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).
1)

    
{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"}
Returns:
{"....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
The cursor spins round and round, but never actually paints any pixels. The result is an empty canvas.
2)

    
{"FORWARD 1"}
Returns:
{"X...................",
 "X...................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
Going forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted.
3)

    
{"LEFT", "FORWARD 19", "LEFT", "LEFT", "LEFT",
 "FORWARD 18", "LEFT", "LEFT", "LEFT", "FORWARD 17",
 "LEFT", "LEFT", "LEFT", "FORWARD 16", "LEFT",
 "LEFT", "LEFT", "FORWARD 15", "LEFT", "LEFT", "LEFT",
 "FORWARD 14", "LEFT", "LEFT", "LEFT", "FORWARD 13",
 "LEFT", "LEFT", "LEFT", "FORWARD 12", "LEFT", "LEFT",
 "LEFT", "FORWARD 11", "LEFT", "LEFT", "LEFT", "FORWARD 10",
 "LEFT", "LEFT", "LEFT", "FORWARD 9", "LEFT", "LEFT",
 "LEFT", "FORWARD 8", "LEFT", "LEFT", "LEFT", "FORWARD 7"}
Returns:
{"XXXXXXXXXXXXXXXXXXXX",
 "...................X",
 "..XXXXXXXXXXXXXXXX.X",
 "..X..............X.X",
 "..X.XXXXXXXXXXXX.X.X",
 "..X.X..........X.X.X",
 "..X.X.XXXXXXXX.X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.XXXXXXXXXX.X.X",
 "..X.X............X.X",
 "..X.XXXXXXXXXXXXXX.X",
 "..X................X",
 "..XXXXXXXXXXXXXXXXXX",
 "...................." }

代码:

public class DrawLines

{

 

    public string[] execute(string[] commands)

    {

        //down 0,left,1,up,2,right3

        const string FW = "FORWARD";

        const string L = "LEFT";

        char[,] lines = new char[20,20];

        for(int i=0;i<20;i++)

            for(int j=0;j<20;j++)

               lines[i, j] = '.';

 

        int row = 0, col = 0;

        int flag = 0;

        int[,] np = new int[4,2] { {1,0},{0,1},{-1,0},{0,-1} };

        for(int i=0;i<commands.Length;i++)

        {

            if(commands[i] == L)

            {

                flag = (flag + 1) % 4;

            }

 

            if(commands[i].StartsWith(FW) &&

                (commands[i].Length > 8 && commands[i].Length <11) )

            {

                string s = commands[i].Replace(FW, "").Trim();

                if (s == null || s == "")

                    continue;

                try

                {

                    int n = int.Parse(s);

                    for( int k=0;k<n;k++)

                    {

                        if (!(row < 0 || row >= 20 || col < 0 || col >= 20))

                            lines[row, col] = 'X';

                        row = row + np[flag, 0];

                        col = col + np[flag, 1];

 

                        if (!(row < 0 || row >= 20 || col < 0 || col >= 20))

                            lines[row, col] = 'X';

                    }

                   

                }

                catch(FormatException E)

                {

                    continue;

                }

            }

        }

 

        return print(lines,20);

       

    }

 

    public string[] print(char[,] lines,int n)

    {

        string os = "{";

        string[] dirs = new string[20];

        for(int i=0;i<n;i++)

        {

            if (i != 0)

                os += " ";

            os += "/"";

 

            dirs[i] = "";

            for(int j=0;j<n;j++)

            {

                os += lines[i, j];

                dirs[i] += lines[i, j];

            }

            os += "/",/n";

       }

 

       Console.WriteLine(os);

 

       return dirs;

    }

};

=======================================================

以下是今天我参加比赛的二题:

题1(250 points)

Problem Statement
    
You are given a String input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Definition
    
Class:
ReverseSubstring
Method:
findReversed
Parameters:
String
Returns:
String
Method signature:
String findReversed(String input)
(be sure your method is public)
    

Notes
-
The substring and its reversal may overlap partially or completely.
-
The entire original string is itself a valid substring (see example 4).
Constraints
-
input will contain between 1 and 50 characters, inclusive.
-
Each character of input will be an uppercase letter ('A'-'Z').
Examples
0)

    
"XBCDEFYWFEDCBZ"
Returns: "BCDEF"
We see that the reverse of BCDEF is FEDCB, which appears later in the string.
1)

    
"XYZ"
Returns: "X"
The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.
2)

    
"ABCABA"
Returns: "ABA"
The string ABA is a palindrome (it's its own reversal), so it meets the criteria.
3)

    
"FDASJKUREKJFDFASIREYUFDHSAJYIREWQ"
Returns: "FDF"

4)

    
"ABCDCBA"
Returns: "ABCDCBA"
Here, the entire string is its own reversal.

这一题不难,就不写代码了。

题2(750 points)

Problem 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,000, return -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.

这一题我在规定的时间内没有完成。郁闷.......,后来写的代码如下
public class WordPath
{

    private string[,] m_Str = null;

    private int m_Count = 0;

    private int m_Dim = 0;

    private readonly int[,] m_V = new int[8, 2]{{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

   

    private void InitStrArray(string[] grid)

    {

        for (int i = 0; i < m_Dim; i++)

            for (int j = 0; j < m_Dim; j++)

                m_Str[i, j] = grid[i].Substring(j, 1);

    }

 

    public int countPaths(string[] grid, string find)

    {

        m_Dim = grid.Length;

        m_Str = new string[m_Dim,m_Dim];
        InitStrArray(grid);

        string start = find.Substring(0, 1);

 

        for (int i = 0; i < m_Dim; i++)

            for (int j = 0; j < m_Dim; j++)

            {

                if (m_Str[i, j] == start)

                {
                    if (m_Count > 1000000000) return -1;

                    getStrCountRecursion(i, j, find,m_Str[i, j]);

                }

            }

       

        return m_Count;

    }

 

    private void getStrCountRecursion(int x, int y,string find,string search)

    {

      

        int x1, y1;

        string sTmp = "";

        for(int i=0;i<8;i++)

        {

            x1 = x + m_V[i, 0]; y1 = y + m_V[i, 1];

            if(x1 >=0 && x1 < m_Dim && y1 >= 0 && y1 < m_Dim)

            {

                sTmp = search + m_Str[x1, y1];

                if (sTmp.Length == find.Length)

                {

                    if(sTmp == find)

                    {

                        m_Count++;

                    }              

                }

                else

                {

                   getStrCountRecursion(x1, y1, find, sTmp);

                }                              

            }

        }

 

        return;   

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值