简体   繁体   English

C# 动态规划转换

[英]C# Dynamic programming conversion

Hi I have a task to convert exponential code into linear, but I have no idea how to do it.嗨,我的任务是将指数代码转换为线性代码,但我不知道该怎么做。 Could you give me any tips or point me in the right direction?你能给我任何提示或指出正确的方向吗?

Here's the code:这是代码:

        int F (int m, int n)
        {
            if(n == 0)
            {
                return m;
            }

            else if(m == 0 && n > 0)
            {
                return n;
            }

            else
            {
                int[] array = { 1 + F(m - 1, n), 1 + F(m, n - 1), D(m, n) + F(m - 1, n - 1) };
                return array.Min();
            }
        }

        int D(int i, int f)
        {
            if(x[i] == y[f])
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

Update:更新:

Am I going in the right direction?我是否朝着正确的方向前进? So far it works only with m=0,1,2 and n=0,1,2.到目前为止,它仅适用于 m=0,1,2 和 n=0,1,2。 How do I fill all the values if let's say, I give m = 10 and n = 20?如果假设我给出 m = 10 和 n = 20,我该如何填写所有值?

int Fdp(int m, int n)
        {
            fdin[m, 0] = m;

            for(int i = 0; i <= n; i++)
            {
                fdin[0, i] = n;
            }

            if (n == 0)
            {
                return m;
            }

            else if (m == 0 && n > 0)
            {
                return n;
            }

            else
            {

                int[] temp = { 1 + fdin[m-1, n], 1+ fdin[m,n-1], D(m,n) + fdin[m-1,n-1] };
                fdin[m, n] = temp.Min();
                return temp.Min();
            }

        }

Solved it.解决了。

        static int Fdp(int m, int n)
        {
            for (int i = 0; i <= m; i++)
            {
                fdin[i, 0] = i;
                for (int j = 1; j <= n; j++)
                {
                    if(i == 0)
                    {
                        fdin[i, j] = j;
                    }
                    else
                    {
                        int[] temp = new int[] { 1 + fdin[i - 1, j], 1 + fdin[i, j - 1], D(i, j) + fdin[i - 1, j - 1] };
                        fdin[i, j] = temp.Min();
                    }
                }
            }
            return fdin[m,n];
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM