資料結構›Ch1 演算法基礎
第 22 題/共 57 題
◀ DS 22/57
22. Permutation、Algorithm Correctness、Time Complexity
#DS-01-022中PermutationAlgorithm CorrectnessTime Complexity

(6%) This problem considers permutations of An={0,1,…,n−1}A_n = \{0,1,\ldots,n-1\}. Let PP be a permutation from AnA_n. PP can be represented as a 0-1 string SS of length n−1n-1, where:

  • S[i]=′1′S[i] = '1' if P[i]<P[i+1]P[i] < P[i+1], and
  • S[i]=′0′S[i] = '0' if P[i]>P[i+1]P[i] > P[i+1].

For example, a permutation P=(0,1,3,2)P = (0, 1, 3, 2) can be represented as a string 110. Note that this representation is not one to one, i.e., different permutations may be represented as the same string. Given a 0-1 string SS, we are interested in reconstructing a possible permutation PP.

Consider the following C++ procedure.

vector<int> findPermutation(string S) {
        int left = count(S.begin(), S.end(), '0'), right = left;
        vector<int> res = {left};
        for (char &c : S)
                res.push_back(c == '1' ? ++right : --left);
        return res;
}

Which of the following statements is/are correct?

📄 交大114
跳轉到第題
▤完整推導請見《WH 資工筆記 · 資料結構》Ch1 演算法基礎
本章題號 · 21–40 / 57