예제 #1
0
            private LineBreakResult FindLastOptimalBreak(int j)
            {
                if (m_cache[j] != null)
                {
                    return(m_cache[j]);
                }

                int cost = GetCost(0, j);

                if (cost < m_infinity)
                {
                    return(new LineBreakResult(cost, -1));
                }

                LineBreakResult min = new LineBreakResult();

                for (int k = 0; k < j; k++)
                {
                    int result = FindLastOptimalBreak(k).Cost + GetCost(k + 1, j);
                    if (result < min.Cost)
                    {
                        min.Cost = result;
                        min.K    = k;
                    }
                }

                m_cache[j] = min;
                return(min);
            }
예제 #2
0
            public OptimalWordWrappedString(string s, int lineWidth)
            {
                string[] lines = s.Split('\n');
                for (int c = 0; c < lines.Length; c++)
                {
                    m_str       = lines[c].Trim();
                    m_lineWidth = lineWidth;

                    BuildWordList(m_str, m_lineWidth);
                    m_costCache = new int[m_wordList.Length, m_wordList.Length];
                    for (int x = 0; x < m_wordList.Length; x++)
                    {
                        for (int y = 0; y < m_wordList.Length; y++)
                        {
                            m_costCache[x, y] = -1;
                        }
                    }

                    m_cache = new LineBreakResult[m_wordList.Length];

                    Stack <int> stack = new Stack <int>();

                    LineBreakResult last = new LineBreakResult(0, m_wordList.Length - 1);
                    stack.Push(last.K);
                    while (last.K >= 0)
                    {
                        last = FindLastOptimalBreak(last.K);
                        if (last.K >= 0)
                        {
                            stack.Push(last.K);
                        }
                    }

                    int start = 0;
                    while (stack.Count > 0)
                    {
                        int next = stack.Pop();
                        m_result.Append(GetWords(start, next));
                        if (stack.Count > 0)
                        {
                            m_result.Append(Environment.NewLine);
                        }
                        start = next + 1;
                    }

                    if (c != lines.Length - 1)
                    {
                        m_result.Append(Environment.NewLine);
                    }
                }

                m_wordList  = null;
                m_cache     = null;
                m_str       = null;
                m_costCache = null;
            }
예제 #3
0
            private LineBreakResult FindLastOptimalBreak(int j)
            {
                if (mfCache[j] != null)
                {
                    return mfCache[j];
                }

                int cost = GetCost(0, j);
                if (cost < mInfinity)
                {
                    return new LineBreakResult(cost, -1);
                }

                LineBreakResult min = new LineBreakResult();
                for (int k = 0; k < j; k++)
                {
                    int result = FindLastOptimalBreak(k).Cost + GetCost(k + 1, j);
                    if (result < min.Cost)
                    {
                        min.Cost = result;
                        min.K = k;
                    }
                }

                mfCache[j] = min;
                return min;
            }
예제 #4
0
            public OptimalWordWrappedString(string s, int lineWidth)
            {
                string[] lines = s.Split('\n');
                for (int c = 0; c < lines.Length; c++)
                {
                    mStr = lines[c].Trim();
                    mLineWidth = lineWidth;

                    BuildWordList(mStr, mLineWidth);
                    mCostCache = new int[mWordList.Length, mWordList.Length];
                    for (int x = 0; x < mWordList.Length; x++)
                        for (int y = 0; y < mWordList.Length; y++)
                            mCostCache[x, y] = -1;

                    mfCache = new LineBreakResult[mWordList.Length];

                    IStack<int> stack = new ArrayList<int>();

                    LineBreakResult last = new LineBreakResult(0, mWordList.Length - 1);
                    stack.Push(last.K);
                    while (last.K >= 0)
                    {
                        last = FindLastOptimalBreak(last.K);
                        if (last.K >= 0)
                            stack.Push(last.K);
                    }

                    int start = 0;
                    while (!stack.IsEmpty)
                    {
                        int next = stack.Pop();
                        mResult.Append(GetWords(start, next));
                        if (!stack.IsEmpty)
                            mResult.Append(Environment.NewLine);
                        start = next + 1;
                    }

                    if (c != lines.Length - 1)
                        mResult.Append(Environment.NewLine);
                }

                mWordList = null;
                mfCache = null;
                mStr = null;
                mCostCache = null;
            }