Algorithm 计算不同子序列的时间复杂度

Algorithm 计算不同子序列的时间复杂度,algorithm,time-complexity,Algorithm,Time Complexity,问题是,我想出了下面的代码,但我很难找出它的时间复杂性。知道如何计算它的时间复杂度吗?(如果没有字典内存怎么办) public int NumDistinct(字符串s,字符串t) { if(string.IsNullOrEmpty(s)和&string.IsNullOrEmpty(t)) 返回1; else if(string.IsNullOrEmpty(s)| | string.IsNullOrEmpty(t)) 返回0; 返回FindSequences(s,0,t,0); } 字典记忆=新

问题是,我想出了下面的代码,但我很难找出它的时间复杂性。知道如何计算它的时间复杂度吗?(如果没有字典内存怎么办)

public int NumDistinct(字符串s,字符串t)
{
if(string.IsNullOrEmpty(s)和&string.IsNullOrEmpty(t))
返回1;
else if(string.IsNullOrEmpty(s)| | string.IsNullOrEmpty(t))
返回0;
返回FindSequences(s,0,t,0);
}
字典记忆=新字典();
私有int-FindSequences(字符串s、int-idxs、字符串t、int-idxt)
{
if(idxt==t.Length)
返回1;
else if(idxs==s.Length)
返回0;
string key=string.Format(“{0}-{1}”,idxs,idxt);
if(memomery.ContainsKey(键))
归还回忆录[钥匙];
int结果=0;
如果(s[idxs]==t[idxt]){
结果=FindSequences(s,idxs+1,t,idxt+1)+FindSequences(s,idxs+1,t,idxt);
}否则{
结果=查找序列(s,idxs+1,t,idxt);
}
添加(键、结果);
返回结果;
}

这里的时间复杂度是
O(SizeOf(s)*SizeOf(t))
。对于
动态编程解决方案
,您可以使用不同状态的数量来计算时间复杂度,这里的状态数量是
SizeOf(s)*SizeOf(t)

动态规划使用了
记忆的概念
,即存储一个状态的结果,以便在遇到相同的状态时可以使用它,这样我们就可以有效地不进行冗余计算,因为状态经常重复,当它们重复时,我们使用以前计算的结果来降低时间复杂度

还要注意,时间复杂度还取决于
查找表
DP表
,在上述情况下,这是一个
字典
,因此您还必须考虑
字典查找
字典插入
的时间,有效地使复杂度为:


O(SizeOf(s)*SizeOf(t)*查字典或插入字典的时间)

如果我们删除“字典记忆”,时间复杂度是多少?因为您正在删除
记忆步骤
,时间复杂度将变成
指数
,即在最坏的情况下:在递归调用的每个步骤中,您可以做出两个决定,即在答案中是否包含字符串
t
的当前字母表,您可以通过将
idxt
变量增加一个或保持不变来实现这一点,因此您有
2^n
生成搜索空间的可能方法,其中
n
是字符串
t
的大小。
        public int NumDistinct (string s, string t)
        {
            if (string.IsNullOrEmpty (s) && string.IsNullOrEmpty (t))
                return 1;
            else if (string.IsNullOrEmpty (s) || string.IsNullOrEmpty (t))
                return 0;

            return FindSequences (s, 0, t, 0);
        }

        Dictionary<string, int> memoery = new Dictionary<string, int> ();

        private int FindSequences (string s, int idxs, string t, int idxt)
        {
            if (idxt == t.Length)
                return 1;
            else if (idxs == s.Length)
                return 0;

            string key = string.Format ("{0}-{1}", idxs, idxt);
            if (memoery.ContainsKey (key))
                return memoery [key];

            int result = 0;
            if (s [idxs] == t [idxt]) {
                result = FindSequences (s, idxs + 1, t, idxt + 1) + FindSequences (s, idxs + 1, t, idxt);
            } else {
                result = FindSequences (s, idxs + 1, t, idxt);
            }
            memoery.Add (key, result);
            return result;
        }