《中国编程挑战赛 资格赛》解答

gcjc_logo.gif

老是提交不上代码,搞得最后提交成功的时候居然超时,郁闷中.....

以下为我的解,希望大家提出更好的解决方案!

250分题
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.
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.

我的答案如下:

    using System;

    public class ReverseSubstring
    {
        public string findReversed(string input)
        {
            char[] m_InputChars=input.ToCharArray();
            int m_Length=m_InputChars.GetLength(0);
            char[] m_ReversalInputChars = new char[m_Length];
          

            for (int i = 0, j = m_Length-1; i < m_Length; i++,j--)
            {
                m_ReversalInputChars[j]=m_InputChars[i];
            }
            //abc
            //__abc__
            char[] m_Box1 = new char[(m_Length - 1) * 2 + m_Length];
            for (int i = m_Length - 1, j = 0; i < 2 * m_Length - 1; i++, j++)
            {
                m_Box1[i] = m_InputChars[j];
            }
            char[] m_Box2 = new char[(m_Length - 1)*2+m_Length];
           

            string m_SameChars = string.Empty;

            //开始的位置
            for (int i = 0; i < m_Length*2-1; i++)
            {
               
                //放置反序单词
                for (int j = 0; j < m_Length; j++)
                {
                    m_Box2[i + j] = m_ReversalInputChars[j];
                }

                //开始循环比较
                string m_SameCharsTemp = string.Empty;
                bool m_MatchBegin = false;

               

                //循环计较
                for (int h = 0; h < (m_Length - 1) * 2 + m_Length; h++)
                {
                    if (m_MatchBegin && (m_Box1[h] != m_Box2[h] || (m_Box1[h]=='\0' && m_Box2[h]=='\0')))
                    {
                        if (m_SameChars.Length < m_SameCharsTemp.Length || m_SameChars.Length == 0)
                        {

                            m_SameChars = m_SameCharsTemp;
                        }

                        m_SameCharsTemp = string.Empty;
                        m_MatchBegin = false;
                    }

                    if (m_Box1[h] == m_Box2[h] && m_Box1[h] != '\0')
                    {

                        m_SameCharsTemp += m_Box1[h];
                        if (h == (m_Length - 1) * 2 + m_Length-1)
                        {
                            m_SameChars = m_SameCharsTemp;
                        }
                        m_MatchBegin = true;
                    }
                }

                //清空
                for (int j = 0; j < m_Length; j++)
                {
                    m_Box2[i + j] = '\0';
                }
            }

            return m_SameChars;
        }
    }

750分题
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.
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.

我的答案:
    using System;

    public class WordPath
    {
        private char[,] m_newgrid;
        private int v;
        char[] findchars;
        public int countPaths(string[] grid, string find)
        {
            int m_CountPaths=0;
            v=grid.GetLength(0);
            m_newgrid = new char[v,v];
            for (int i = 0; i < grid.Length;i++)
            {
                string m_row = grid[i];
                char[] m_charsinrow = m_row.ToCharArray();
                for (int j = 0; j < m_charsinrow.GetLength(0); j++)
                {
                    m_newgrid[j, i] = m_charsinrow[j];
                }
            }

            findchars = find.ToCharArray();

            //寻找出发点
            for (int i = 0; i < v; i++)
            {
                for (int j = 0; j < v; j++)
                {
                    if (m_newgrid[i, j] == findchars[0])
                    {
                        int nextcharid = 1;
                        nextPath(i, j, nextcharid, ref m_CountPaths);
                    }
                }
            }
            
            if (m_CountPaths > 1000000000)
            {
                m_CountPaths = -1;
            }

            return m_CountPaths;
        }

        //根据现在所在位置和下一个字符得到下一个位置
        private void nextPath(int startx, int starty, int nextcharid, ref int m_CountPaths)
        {
            if (nextcharid == findchars.GetLength(0))
            {
                m_CountPaths++;
                return;
            }

            //从左上开始查找下一个字符
            Go(startx, starty, -1, -1, nextcharid, ref m_CountPaths);
            Go(startx, starty, 0, -1, nextcharid, ref m_CountPaths);
            Go(startx, starty, 1, -1, nextcharid, ref m_CountPaths);
            Go(startx, starty, 1, 0, nextcharid, ref m_CountPaths);
            Go(startx, starty, 1, 1, nextcharid, ref m_CountPaths);
            Go(startx, starty, 0, 1, nextcharid, ref m_CountPaths);
            Go(startx, starty, -1, 1, nextcharid, ref m_CountPaths);
            Go(startx, starty, -1, 0, nextcharid, ref m_CountPaths);
        }

        private void Go(int startx, int starty, int gox, int goy, int nextcharid, ref int m_CountPaths)
        {
            int m_searchx=startx+gox;
            int m_searchy=starty+goy;
            if (m_searchx>=0 && m_searchx<v && m_searchy>=0 && m_searchy<v)
            {
                if (m_newgrid[m_searchx, m_searchy] == findchars[nextcharid])
                {
                   
                    nextcharid++;

                    nextPath(m_searchx, m_searchy, nextcharid, ref m_CountPaths);
                }
            }
        }
    }

转载于:https://www.cnblogs.com/csharphack/archive/2005/12/12/295855.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值