Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 确定代码的运行时间_C++_Dynamic Programming_Asymptotic Complexity - Fatal编程技术网

C++ 确定代码的运行时间

C++ 确定代码的运行时间,c++,dynamic-programming,asymptotic-complexity,C++,Dynamic Programming,Asymptotic Complexity,我今天已经编写了以下dp代码,它工作得很好,因为它在提交时获得了一些分数()。但是,我无法确定代码的运行时间。我觉得它是O(n^2*logd),但我无法证明它 class Solution { public: unordered_map<string, bool> m; bool wordBreak(string s, unordered_set<string>& wordDict) { int n = s.length();

我今天已经编写了以下dp代码,它工作得很好,因为它在提交时获得了一些分数()。但是,我无法确定代码的运行时间。我觉得它是
O(n^2*logd)
,但我无法证明它

class Solution {
public:
    unordered_map<string, bool> m;
    bool wordBreak(string s, unordered_set<string>& wordDict) {
        int n = s.length();
        string t = "";
        for(int i=0;i<n;i++){
            t += s.at(i);
            //cout << t << endl;
            if(wordDict.find(t) != wordDict.end()){
                m[t] = true;
                string x = "";
                for(int j=i+1;j<n;j++){
                    x += s.at(j);
                }
                if(x == ""){
                    return true;
                }
                if(m.find(x) != m.end()){
                    if(m[x] == true){
                        return true;
                    }else{
                        continue;
                    }
                }else{
                    if(wordBreak(x, wordDict)){
                        m[x] = true;
                        return true;
                    }else{
                        //cout << x << endl;
                        m[x] = false;
                        continue;
                    }
                }
            }else{
                //m[t] = false;
            }
        }
        return false;
    }
};
类解决方案{
公众:
无序地图m;
布尔断字(字符串s、无序集和wordDict){
int n=s.长度();
字符串t=“”;

对于(int i=0;i首先,我将重写如下(未测试):

类解决方案{
公众:
无序地图m;
布尔断字(字符串s、无序集和wordDict)
{
而(!s.empty())
{
int n=s.size();
对于(int i=0;i它似乎有
O(n*n)
复杂性。你使用记忆,你的算法的每一步都在
m
中产生至少1个新值。任何字符串中都有
n*n/2
子字符串,所以在最坏的情况下,你会找到包含n*n/2段的整个字符串的解决方案

PS:考虑无序的映射图与<代码> O(1)< /代码> ./P> 编辑:


在你的情况下,用On(n)来考虑无序的映射是比较合适的。<代码> M.Debug 将需要为它的参数计算哈希值,即字符串。如果存储索引而不是字符串本身,它可能工作得更快。

< P>这里是我是如何解决的。

bool wordBreak(string s, unordered_set<string>& wordDict) {
    if (s.empty()) return true;

    vector<bool> dp(s.size(), false);
    for (int i = 0; i < dp.size(); ++i)
    {
        for (int j = i; j >= 0; --j)
        {
            if (wordDict.find(s.substr(j, i - j + 1)) != wordDict.end() &&
                (j == 0 || dp[j - 1]))
            {
                dp[i] = true;
                break;
            }
        }
    }

    return dp.back();
}
bool断字(字符串s、无序集和wordDict){
如果(s.empty())返回true;
向量dp(s.size(),false);
对于(int i=0;i=0;--j)
{
if(wordDict.find(s.substr(j,i-j+1))!=wordDict.end()&&
(j==0 | | dp[j-1]))
{
dp[i]=真;
打破
}
}
}
返回dp.back();
}

衡量这一点通常包括检测代码或计数器,以查看您在代码中执行特定操作的次数(迭代、比较等)使用不同的
N
D
等值,并将结果绘制在结果图中,或者使用不同的
N
D
值测量执行所需的时间。此外
O(x)
给出了“主导
x
”,因此,除非
logd
非常大,而N很小,
n2
将占主导地位,因此该值为
O(N^2)
,即使存在,也会涉及
*logd
s.substr(0,i)
-此操作是
O(i)
,您可以为所有
0执行此操作
bool wordBreak(string s, unordered_set<string>& wordDict) {
    if (s.empty()) return true;

    vector<bool> dp(s.size(), false);
    for (int i = 0; i < dp.size(); ++i)
    {
        for (int j = i; j >= 0; --j)
        {
            if (wordDict.find(s.substr(j, i - j + 1)) != wordDict.end() &&
                (j == 0 || dp[j - 1]))
            {
                dp[i] = true;
                break;
            }
        }
    }

    return dp.back();
}