在for循环中存储某些子序列(或者可能是递归)

时间:2015-06-29 11:29:38

标签: c# algorithm multidimensional-array jagged-arrays subsequence

任务如下:

1)输入=" n"的序列数字(n - 正数,数字 - 没有限制)

2)目标:提取最长的非递减子序列。如果两个或更多的子序列具有相同的长度 - 打印最左边的。

示例:input = 7 3 5 8 -1 6 7,输出= 3 5 6 7

现在,虽然我一整天都在抨击它,但我遇到了两个主要问题。第一种是编写一种算法,该算法存储来自输入序列的所有可能的非递减子序列。第二种是提出一种测量所有子序列的最佳方法,并打印出最左边的最长子序列。这是我目前所在的地方:

using System;

class LongestNonDecreasingSequence
{
    static void Main()
    {
        string input = Console.ReadLine();
        string[] chars = input.Split(' ');
        int[] numbers = new int[input.Length];
        int n = chars.Length;
        for(int i = 0; i < n; i++)
        {
            numbers[i] = int.Parse(chars[i]);
        }
        int[,] matrix = new int[n, n];
        for(int column = 0; column < n; column++)
        {
            for(int row = column, rowMatrix = 0; row < n; row++)
            {
                if(numbers[column] <= numbers[row])
                {
                    matrix[column, rowMatrix] = numbers[row];
                    rowMatrix++;
                }
            }
        }
        for(int column = 0; column < n; column++)
        {
            for(int row = 1; row < n; row++)
            {
                if(matrix[column, row] < matrix[column, row - 1] && /* ?Condition */)
                {
                    /* Remove ?elements */
                }
                /* ?Remove rows, cointaining zero value */
            }
        }
        int[] matrixLengths = new int[n];
        for(int column = 0; column < n; column++)
        {
            matrixLengths[column] = matrix.GetLength(1);
        }   
    }
}

我有一个2D数组,将每个子序列存储在一个新列中。在我们的例子中 - 7 3 5 8 -1 6 7;在第0列中,我们存储7,第1列 - 第7列,第2列 - 3 5 8 6 7,第3 - 5 8 8 7列,第4 - 8列,第5列 - 第1 6 6行,第6 - 6列,第7列7 - 7;

现在问题1)在每个子序列中删除数字,不符合条件。在我们的案例中:第2栏 - 3 5 6 7,第3栏 - 5 6 7;

现在问题2)通过Array.Length比较所有列(1 - 7)并获得最高值。

我仍然无法找出问题1的算法。对于问题2,我只需要知道如何测量2D数组中每个单独列的长度。从过去几小时我一直在阅读的内容来看,也许我不得不使用锯齿状阵列而不是2D。获得每列的长度会更容易吗?在for循环中定义每个下一个锯齿状数组(每列的数组)会比较棘手,但我想我可以使用字符串来保存我的数字,然后使用&#39; .Split&#39;为数组赋值。

我希望我表达得足够清楚。如果不是这样,请通知我,我可以指定。我们非常欢迎任何有关这两个问题的建议和信息。

谢谢!

0 个答案:

没有答案