【发布时间】:2015-09-30 07:51:18
【问题描述】:
我今天编写了以下 dp 代码,它运行良好,因为它获得了一些提交点 (here is the problem)。但是我无法确定我的代码的运行时间。我觉得它是O(n^2 * log D),但我无法证明这一点。
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;
}
};
【问题讨论】:
-
通常测量这涉及到检测代码将计数器以查看您在代码中使用不同值
N、D等执行某件事的次数(迭代、比较等) , 并将结果绘制在结果图中,或者执行相同的操作,但使用N和D的不同值测量执行所需的时间。同样O(x)给出“占主导地位的x”,所以除非log D很大,而N 很小,否则N^2将占主导地位,所以值为O(N^2),即使存在,技术上也是@987654333 @参与)。
标签: c++ dynamic-programming asymptotic-complexity