Exemple #1
0
Array2D CombinationWithDuplicatedElementsByNonRecursion(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }

    std::sort(array.begin(), array.end());
    result.push_back(Array1D());
    int last = array[0], opt_result_num = 1;
    for (int i = 0; i < array.size(); i++) {
        if (array[i] != last) {
            last = array[i];
            opt_result_num = result.size();
        }
        Array2D::size_type result_size = result.size();
        for (int j = result_size - 1; j >= result_size - opt_result_num; j--) {
            result.push_back(result[j]);
            result.back().push_back(array[i]);
        }
    }
    return result;
}
Exemple #2
0
Array2D SetOfStacks(Array2D ope, int size)
{
    // write code here

    if (ope.empty()) { return Array2D(); }
    if (size <= 0) { assert(false); }
    /** @brief has no bugs, but too complex. */
//    stack<Array1D> temp;
//    Array1D cur_array(size);
//    int cur_index = -1;
//    for (int i = 0; i < ope.size(); i++) {
//        if (ope[i][0] == 1) {
//            if (cur_index == size - 1) {
//                temp.push(cur_array);
//                cur_array = Array1D(size);
//                cur_index = -1;
//            }
//            cur_array[++cur_index] = ope[i][1];
//        } else if (ope[i][0] == 2) {
//            if (cur_index == -1) {
//                if (temp.empty()) { return Array2D(); }
//                cur_array = temp.top();
//                temp.pop();
//                cur_index = size - 1;
//            }
//            cur_index--;
//        } else { return Array2D(); }
//    }
//
//    Array2D result(temp.size() + 1);
//    if (cur_index > -1) {
//        result[temp.size()] = Array1D(&cur_array[0], &cur_array[cur_index] + 1);
//    }
//    cur_index = temp.size() - 1;
//    while(!temp.empty()) {
//        result[cur_index--] = temp.top();
//        temp.pop();
//    }
    /** @todo don't pass all test cases */
    Array2D result;
    Array1D cur_array;
    for (Array2D::iterator it = ope.begin(); it != ope.end(); ++it) {
        if ((*it)[0] == 1) {
            if (cur_array.size() == size) {
                result.push_back(cur_array);
                cur_array.clear();
            }
            cur_array.push_back((*it)[1]);
        } else if ((*it)[0] == 2){
            if (cur_array.empty()) {
                if (result.empty()) { assert(false); }
                cur_array = result.front();
                result.pop_back();
            }
            cur_array.pop_back();
        } else { assert(false); }
    }
    if (!cur_array.empty()) { result.push_back(cur_array); }
    return result;
}
Exemple #3
0
static void CombinationWithDuplicatedElementsVer2Helper(Array1D &array, int left,
                                                        int k, int times,
                                                        Array1D &temp,
                                                        Array2D &result)
{
    if (array.size() - left < k) { return; }
    if (k == 0) {
        result.push_back(temp);
        return;
    }
    for (int i = left; i <= array.size(); i++) {
        if (i == 0 || array[i] != array[i - 1]) {
            times = 1;
            temp.push_back(array[i]);
            CombinationWithDuplicatedElementsVer2Helper(array, i + 1, k - 1, 1, temp, result);
            temp.pop_back();
        }
        else {
            times++;
            if (temp.size() >= times - 1 && temp[temp.size() - times + 1] == array[i]) {
                temp.push_back(array[i]);
                CombinationWithDuplicatedElementsVer2Helper(array, i + 1, k - 1, times, temp, result);
                temp.pop_back();
            }
        }
    }
}
Exemple #4
0
static void CombinationWithDuplicatedElementsHelper(Array1D &array, int left, int k,
                                                    int times, Array1D &temp, Array2D &result)
{
    assert(left >= 0);

    if (array.size() - left < k) { return; }
    if (k == 0) {
        result.push_back(temp);
        return;
    }
    if (left == 0 || array[left] != array[left - 1]) {
        temp.push_back(array[left]);
        CombinationWithDuplicatedElementsHelper(array, left + 1, k - 1, 1, temp, result);
        temp.pop_back();
        CombinationWithDuplicatedElementsHelper(array, left + 1, k, 1, temp, result);
    }
    else {
        if (temp.size() >= times && temp[temp.size() - times] == array[left]) {
            temp.push_back(array[left]);
            CombinationWithDuplicatedElementsHelper(array, left + 1, k - 1, times + 1, temp, result);
            temp.pop_back();
        }
        CombinationWithDuplicatedElementsHelper(array, left + 1, k, times + 1, temp, result);
    }
}
Exemple #5
0
Array2D CombinationVer2(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp;
    CombinationVer2Helper(array, 0, array.size() - 1, K, temp, result);
}
Exemple #6
0
/**
 * @brief find all permutation of array
 * @brief array has some repeating elements
 */
Array2D PermutationUnique(Array1D array)
{
    Array2D result;
    if (array.empty()) { return result; }
    if (array.size() == 1) {
        result.push_back(array);
        return result;
    }
    PermutationUniqueHelper(array, 0, result);
    return result;
}
Exemple #7
0
Array2D CombinationByDecrease(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp(K);
    CombinationByDecreaseHelper(array, array.size(), K, temp, result);
}
Exemple #8
0
Array2D CombinationWithDuplicatedElementsVer2(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp;

}
Exemple #9
0
Array2D Combination(Array1D array, int K) {
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);
        return result;
    }
    Array1D temp;
    CombinationHelper(array, 0, array.size() - 1, K, temp, result);
    return result;
}
Exemple #10
0
static void PermutationHelper(Array1D &array, int index, Array2D &result)
{
    if (index == array.size()) {
        result.push_back(array);
        return;
    }
    for (int i = index; i < array.size(); ++i) {
        std::swap(array[index], array[i]);
        PermutationHelper(array, index + 1, result);
        std::swap(array[index], array[i]);
    }
}
Exemple #11
0
Array2D CombinationByNonRecursion(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D cur(1);
    for (int i = 1; i <= array.size(); i++) {
        Array1D::size_type size = cur.size();
        Array1D temp;
        /** @bug has some problems */
        for (int j = 0; j < size; j++) {
            temp.push_back(cur[j]);
            temp.push_back(array[i]);
            if (temp.size() == K) { result.push_back(temp); }
            else { cur.push_back(array[i]); }
        }
    }
    return result;
}
Exemple #12
0
Array2D CombinationByBinary(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    /** @warning sort or not */
    Array1D temp;
    int begin = (1 << K) - 1, end = (1 << array.size()) - (1 << (array.size() - K));
    for (int bit = begin; bit <= end; bit = NextBit(bit)) {
        temp.clear();
        for (int i = 0; i < array.size(); i++) {
            if (bit & (1 << i)) {
                temp.push_back(array[i]);
            }
        }
        result.push_back(temp);
    }
    return result;
}
Exemple #13
0
static void CombinationByDecreaseHelper(Array1D &array, int left, int count,
                                        Array1D &temp, Array2D &result)
{
    for (int i = left; i >= count; i--) {
        temp[count - 1] = array[i - 1];
        if (count > 1) {
            CombinationByDecreaseHelper(array, i - 1, count - 1, temp, result);
        }
        else {
            result.push_back(temp);
        }
    }
}
Exemple #14
0
static void CombinationByIncreaseHelper(Array1D &array, int left, int count,
                                        Array1D &temp, Array2D &result)
{
    for (int i = left; i < array.size() + 1 - count; i++) {
        temp[count - 1] = array[i];
        if (count - 1 == 0) {
            result.push_back(temp);
        }
        else {
            CombinationByIncreaseHelper(array, i + 1, count - 1, temp, result);
        }
    }
}
Exemple #15
0
static void PermutationUniqueHelper(Array1D &array, int index, Array2D &result)
{
    if (index == array.size()) {
        result.push_back(array);
        return;
    }
    for (int i = index; i < array.size(); i++) {
        if (HasSameElement(array, index, i, array[i])) {
            std::swap(array[index], array[i]);
            PermutationUniqueHelper(array, index + 1, result);
            std::swap(array[index], array[i]);
        }
    }
}
Exemple #16
0
static void CombinationHelper(Array1D &array, int left, int right, int k,
                              Array1D &temp, Array2D &result)
{
    assert(!array.empty());
    assert(left >= 0 && right >= 0);

    if (right - left + 1 < k) { return; }
    if (k == 0) {
        result.push_back(temp);
        return;
    }
    temp.push_back(array[left]);
    CombinationHelper(array, left + 1, right, k - 1, temp, result);
    temp.pop_back();
    CombinationHelper(array, left + 1, right, k, temp, result);
}
Exemple #17
0
static void CombinationVer2Helper(Array1D &array, int left, int right, int k,
                                  Array1D &temp, Array2D &result)
{
    assert(!array.empty());
    assert(left >= 0 && right >= 0);

    if (k == 0) {
        result.push_back(temp);
        return;
    }

    for (int i = left; i <= right - k + 1; i++) {
        temp.push_back(array[i]);
        CombinationVer2Helper(array, i + 1, right, k - 1, temp, result);
        temp.pop_back();
    }
}
Array2D ComputeIDX(const Mat& IDX) {
    assert(IDX.type() == CV_32S);
    assert(IDX.rows > 0 && IDX.cols > 0);

    std::unordered_map<int, Array1D> hash;
    std::vector<int> hash_keys;
    int idx_num = 0;
    for (int i = 0; i < IDX.cols; i++) {
        if (!hash.count(IDX.at<int>(i))) {
            hash_keys.push_back(IDX.at<int>(i));
        }
        hash[IDX.at<int>(i)].push_back(i + 1);
    }
    std::sort(hash_keys.begin(), hash_keys.end());
    Array2D result;
    for (int i = 0; i < hash_keys.size(); i++) {
        result.push_back(hash[hash_keys[i]]);
    }
    return result;
}