PlanStage::StageState NearStage::work(WorkingSetID* out) { ++_stats->common.works; WorkingSetID toReturn = WorkingSet::INVALID_ID; Status error = Status::OK(); PlanStage::StageState nextState = PlanStage::NEED_TIME; // // Work the search // if (SearchState_Initializing == _searchState) { nextState = initNext(); } else if (SearchState_Buffering == _searchState) { nextState = bufferNext(&toReturn, &error); } else if (SearchState_Advancing == _searchState) { nextState = advanceNext(&toReturn); } else { invariant(SearchState_Finished == _searchState); nextState = PlanStage::IS_EOF; } // // Handle the results // if (PlanStage::FAILURE == nextState) { *out = WorkingSetCommon::allocateStatusMember(_workingSet, error); } else if (PlanStage::ADVANCED == nextState) { *out = toReturn; ++_stats->common.advanced; } else if (PlanStage::NEED_FETCH == nextState) { *out = toReturn; ++_stats->common.needFetch; } else if (PlanStage::NEED_TIME == nextState) { ++_stats->common.needTime; } else if (PlanStage::IS_EOF == nextState) { _stats->common.isEOF = true; } return nextState; }
//***************************************************************************** // Initialize the ShiftMatrix values. //***************************************************************************** void ShiftMatrix::init() { // The usedValues bitmap is used to keep track of which values were already // used by the algorithm, and which still need to be used. NABitVector usedValues(heap_); // Later on, we use the nextUsed() method to find the next value that was not // yet used. Since there is no nextNotUsed() method, we are using the bitmap // negatively - a set bit corresponds to a value that was not yet used. for (CollIndex i=0; i<elements_; i++) usedValues.insert(i); // Start with all bits set. // Init the entire matrix initNext(usedValues, 0, combinations_-1, elements_); }
//***************************************************************************** // This is a recursive method used to initialize the matrix a column at a time. // First divide the array of combinations to a number of segments, one for each // possible value left. Fill the next matrix entry for each segment with the // next unused value, and call the method recursively for the rest of the values. //***************************************************************************** void ShiftMatrix::initNext(NABitVector& usedValuesV, // Which values were already used UInt32 from, // From which combination to start UInt32 to, // Till which combination to work UInt32 depth) // How many values left { // The number of segments is the number of entries left. UInt32 segments = depth; // The size of each segment is the number od combinations to do, // divided by the number ofd segments. UInt32 segSize = (to - from + 1) / segments; UInt32 nextValue = 0; // Create a copy of the bitmap for this recursion level only (Horizontal) // that is not affected by the recursive calls (Vertical) NABitVector usedValuesH(usedValuesV); // For each segment, for (CollIndex seg=0; seg<segments; seg++) { // Find the next unused value. usedValuesH.nextUsed(nextValue); // Mark it as used in both vertical and horizontal bitmaps. usedValuesV.remove(nextValue); usedValuesH.remove(nextValue); // Calc the first combination of the segment UInt32 segStart = from + seg*segSize; // For each combination in the segment for (UInt32 comb = segStart; comb < segStart+segSize; comb++) { // Translate the matrix value (nextValue) from the range: [0..elements_] // to the shift value in the range: [-(elements_-1)..(elements_-1)] Int32 shiftValue = nextValue - elements_ + depth; // Update the matrix value, starting with element 0. theMatrix_->setElement(comb, elements_ - depth, shiftValue); } // Make the recursive call if (depth > 1) initNext(usedValuesV, // Use the vertical bitmap segStart, // Start of segment segStart + segSize - 1, // End of segment depth - 1); // One less depth level. // Clear the value used in the vertical bitmap, but not the horizontal. usedValuesV.insert(nextValue); } }
int main(int LazyLearner, char**argv){ scanf("%s", &gst); m = strlen(gst); scanf("%d %d", &n, &num); for(int i=1; i<=n; i++){ scanf("%s", &buff); s[i] = strdup(buff); } qsort(s+1, n, sizeof(char*), cmpstr); for(int i=1; i<=num; i++){ scanf("%d %d %d", &q[i].l, &q[i].r, &q[i].k); q[i].id = i; } qsort(q+1, num, sizeof(query), cmp); initNext(); int curR; for(int i=1; i<=num; i++){ if(i == 1 || q[i].l != q[i-1].l){ initLeft(q[i].l); curR = 0; for(int i=1; i<=n; i++){ x[i].first = R[i]; x[i].second = i; } qsort(x+1, n, sizeof(pair), cmpP); for(int i=1; i<=(4 * n); i++) t[i] = 0; } while (curR < n && x[curR + 1].first <= q[i].r){ ++curR; update(1, 1, n, x[curR].second); } q[i].ans = get(1, 1, n, q[i].k); } qsort(q+1, num, sizeof(query), cmpAns); for(int i=1; i<=n; i++){ if(strlen(s[i]) > 10) s[i][10] = '\0'; } for(int i=1; i<=num; i++){ if(q[i].ans == -1) puts("NO SUCH WORD"); else puts(s[q[i].ans]); } return 0; }