zhu*_*008 7 algorithm dynamic-programming combinatorics
字符串中有多少个子字符串?
Why does string x [1:n] have O(n^2) subtrings in the lecture 21 Dynamic Programming III of
6.006 from MIT?
Why is not O(2^n)?
Run Code Online (Sandbox Code Playgroud)
Vik*_*hat 18
简单地,子字符串由两个参数定义,这两个参数[i,j]是原始字符串中子字符串的起始索引和结束索引.现在,0<=i,j<=n作为指标应该是字符串内,总价值i&j每个人都可以有n个这样的所有组合[i,j]将n*n是O(n^2)
给定一串n个元素,
如果从第一个元素开始,则可以形成n个字符串
如果从第二个元素开始,则可以形成n-1个字符串..依此类推...
例如,取1234
1,12,123,1234
2,23,234
3,34
4
正如你所看到的,一共是n + (n-1) + (n-2) ...1即n个元素的总和是n(n+1)/2