コード例 #1
0
        public int PalindromePartition(string s, int k)
        {
            ProbInfo[,] probInfos             = new ProbInfo[s.Length, k + 1];
            PalindromeInfo[,] palindromeInfos = new PalindromeInfo[s.Length, s.Length];

            return(PalindromePartitionAux(0, k, s, probInfos, palindromeInfos));
        }
コード例 #2
0
        private int MiniPalindrome(int index, int j, string s, PalindromeInfo[,] palindromeInfos)
        {
            if (palindromeInfos[index, j] != null)
            {
                return(palindromeInfos[index, j].Result);
            }

            int result = 0;
            int indexR;
            int indexL;
            int count;
            int length = j - index + 1;

            if ((length) % 2 == 0)
            {
                indexR = length / 2 + index;
                indexL = length / 2 - 1 + index;
                count  = length / 2;
            }
            else
            {
                indexR = length / 2 + 1 + index;
                indexL = length / 2 - 1 + index;
                count  = length / 2;
            }

            for (int i = 0; i < count; i++)
            {
                if (s[indexR + i] != s[indexL - i])
                {
                    result++;
                }
            }

            palindromeInfos[index, j]        = new PalindromeInfo();
            palindromeInfos[index, j].Result = result;
            return(result);
        }