简体   繁体   English

是否有任何O(n ^ 2)算法来生成数组的所有子序列?

[英]Is there any O(n^2) algorithm to generate all sub-sequences of an array?

I was wondering if there is any O(n^2) complexity algorithm for generating all sub-sequences of an array. 我想知道是否有任何O(n ^ 2)复杂度算法用于生成数组的所有子序列。 I know an algorithm but it takes O((2^n)*n) time. 我知道一个算法,但它需要O((2 ^ n)* n)时间。

int main() {
    int n; 
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) 
        cin >> a[i];
    int64_t opsize = pow(2,n);
    for (int counter = 1; counter < opsize; counter++) {
        for (int j = 0; j < n; j++) {
           if (counter & (1 << j))
                 cout << a[j] << " ";
        }
        cout << endl;
    }
}

No 没有

There cannot be any algorithm of less than O(2^n) complexity simply because there are O(2^n) sub-sequences. 由于存在O(2^n)个子序列,因此不能存在任何小于O(2^n)复杂度的算法。 You need to print each of them hence the time complexity has to be greater than or equal to O(2^n) . 您需要打印每个,因此时间复杂度必须大于或等于O(2^n)

You can't improve algorithm complexity, but you can improve how stream is used. 您无法提高算法复杂性,但可以改进流的使用方式。 As other answer points out o(n * 2^n) is best you can have. 正如其他答案所指出的那样o(n * 2^n)是最好的。

When you use std::endl you are flushing buffers of streams. 当你使用std::endl你正在刷新流的缓冲区。 To achieve best performance buffers should flush them selves when they are full. 为了获得最佳性能,缓冲区应该在它们满时自我冲洗。 Since each subsequence have to be quite short (at maximum 64 elements), it means that you are flushing stream quite often and have serious performance hit. 由于每个子序列必须非常短(最多64个元素),这意味着您经常冲洗流并且性能受到严重影响。 So replacing std::endl with '\\n' will provide significant performance improvement. 因此,用'\\n'替换std::endl将显着提高性能。

Other tricks which can help improve stream performance: 其他有助于提高流性能的技巧:

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n; 
    cin >> n;

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

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