public int FinLongestOverAll(int[][] mat)
        {
            int result = 1;

            var r = new RectangularArrays();

            int[][] dp = r.ReturnRectangularIntArray(n, n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    dp[i][j] = -1;
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (dp[i][j] == -1)
                    {
                        FindLongestFromACell(i, j, mat, dp);
                    }

                    result = Math.Max(result, dp[i][j]);
                }
            }

            return(result);
        }
Esempio n. 2
0
        int CountPS(char[] str, int n)
        {
            var arr = new RectangularArrays();

            int[][] dp = arr.ReturnRectangularIntArray(n, n);

            bool[][] P = arr.ReturnRectangularBoolArray(n, n);

            for (int i = 0; i < n; i++)
            {
                P[i][i] = true;
            }

            for (int i = 0; i < n - 1; i++)
            {
                if (str[i] == str[i + 1])
                {
                    P[i][i + 1]  = true;
                    dp[i][i + 1] = 1;
                }
            }

            for (int gap = 2; gap < n; gap++)
            {
                for (int i = 0; i < n - gap; i++)
                {
                    int j = gap + i;

                    if (str[i] == str[j] && P[i + 1][j - 1])
                    {
                        P[i][j] = true;
                    }

                    if (P[i][j])
                    {
                        dp[i][j] = dp[i][j - 1] + dp[i + 1][j] + 1 - dp[i + 1][j - 1];
                    }
                    else
                    {
                        dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1];
                    }
                }
            }

            return(dp[0][n - 1]);
        }