bool isNStraightHand(int*hand, int handSz, int W){ qsort(hand, handSz, sizeof(int), cmp); int n = handSz; int k = 0; for(int i=0; i<n; i++){ if(!i||hand[i] != hand[i-1]) s[k++] = newPair(hand[i],1); else s[k-1].second++; } for(int i=0;i<k;i++){ while(s[i].second>0){ if(i+W-1>=k) return false; for(int j=0; j<W; j++){ if(s[i+j].first != s[i].first+j||s[i+j].second<=0) return false; s[i+j].second--; } } } return true; }
int main() { int aux, n, k; scanf("%d %d %d", &aux, &n, &k); getchar(); pair*swaps=malloc((n+2)*sizeof(pair)); int*begin_position= malloc((n + 2) * sizeof(int)); memset(begin_position, 0, (n + 2) * sizeof(int)); int* end_position= malloc((n + 2) * sizeof(int)); memset(end_position, 0, (n + 2) * sizeof(int)); for (int i = 1; i <= n; i++) { int x, y; scanf("%d %d", &x, &y); getchar(); swaps[i] = newPair(x, y); } begin_position[0] = 1; end_position[n + 1] = k; for (int i = 1; i <= n; i++) { int position = begin_position[i - 1]; if (swaps[i].first == position || swaps[i].second == position) begin_position[i] = swaps[i].first + swaps[i].second - position; else begin_position[i] = position; } for (int i = n; i; i--) { int position = end_position[i + 1]; if (swaps[i].first == position || swaps[i].second == position) end_position[i] = swaps[i].first + swaps[i].second - position; else end_position[i] = position; } for (int i = 1; i <= n; i++) { if (begin_position[i - 1] == end_position[i + 1]) { printf("%d\n", i); return 0; } } printf("%d\n", -1); return 0; }
/* Function loads a password file and gets each hexadecimal string line-by-line and attempts to match the each one with the hex strings in entryMap, which must be preloaded from Menu Option 2 (DictionaryLoader). For those that cannot be found in entryMap, the passwords are cracked with the bruteForceSolve method. */ void DictionaryDecrypter::decryptDictionary(bool defaultFilename, std::unordered_map<std::string, DictionaryLoader::entry*> &entryMap, std::map<int, DictionaryDecrypter::SolvedEntry*> &solvedPasswords){ std::string filename; std::list<SolvedEntry*> unsolvedPasswords; int entryNumber = 0; if (!defaultFilename){ std::cout << "Enter custom filename: "; std::getline(std::cin, filename); } else { filename = "pass.txt"; } std::ifstream file(filename); std::ofstream outfile("pass_solved.txt"); if (file.is_open()){ std::cout << "Decrypting " + filename + "...\n"; std::string line; while (std::getline(file, line)){ SolvedEntry * newEntry = new SolvedEntry; newEntry->passwordEntryNumber = entryNumber++; // Increment entryNumber for each entry and store it so the passwords are // outputted correctly after all are decrypted newEntry->hex_str = line; // Store the hexadecimal string if (entryMap.find(line) != entryMap.end()){ // If an entry in the entryMap matches the hex string... newEntry->plainTextSolution = entryMap[line]->word; // Store the plainText solution from the entryMap std::pair<int, SolvedEntry*> newPair(newEntry->passwordEntryNumber, newEntry); // Make a pair of the entryNumber and pointer to the entry solvedPasswords.insert(newPair); } else { unsolvedPasswords.push_back(newEntry); // If a hex doesn't get a match in the entryMap, store in } // list of unsolved passwords to later brute force solve } // Measures performance time of brute force solve LARGE_INTEGER freq, before, after; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&before); // Uses Concurrency to brute force solve multiple passwords // Lambda that passes in solvedPasswords by reference and each of the unsolved passwords into bruteForceSolve Concurrency::parallel_for_each(unsolvedPasswords.begin(), unsolvedPasswords.end(), [this, &solvedPasswords](SolvedEntry* x) { DictionaryDecrypter::bruteForceSolve(x, solvedPasswords); }); QueryPerformanceCounter(&after); float fElapsed = static_cast<float>(after.QuadPart - before.QuadPart) / freq.QuadPart; std::cout << "\nTime elapsed: " << fElapsed << " seconds" << std::endl; std::map<int, SolvedEntry*>::iterator iter; std::string strToReturn; // Once all passwords are decrypted, print them out into the outfile "pass_solved.txt" for (iter = solvedPasswords.begin(); iter != solvedPasswords.end(); ++iter) { strToReturn.append(iter->second->hex_str); // First item is the hexadecimal string strToReturn.append(", "); strToReturn.append(iter->second->plainTextSolution + "\n"); // Second item is the actual password } outfile << strToReturn; std::cout << "Output file pass_solved.txt created. \n\n"; outfile.close(); // Avoid memory leak and delete all SolvedEntrys for (SolvedEntry * x : unsolvedPasswords){ delete x; } } else { std::cout << "File could not be opened." << std::endl << std::endl; } }
/* Function that brute force solves any unsolved passwords by trying every possible permutation for four-letter passwords. Passes in a pointer to an unsolvedPassword entry, and the solvedPasswords map by reference. */ void DictionaryDecrypter::bruteForceSolve(SolvedEntry* unsolvedPassword, std::map<int, DictionaryDecrypter::SolvedEntry*> &solvedPasswords){ // Initialization int digits[4] = { 0, 0, 0, 0 }; char potentialPassword[5] = { NULL }; unsigned char hash[20]; char hex_str[41]; /* Loop through all unsolved passwords and try each permutation four digits long until all passwords are solved. For loop of int i iterates 1679616 times because that is 36^4, the number of permutations of four characters. Digits: [0][1][2][3] Potential Password: [0][1][2][3] */ for (int i = 0; i < 1679616; i++){ if (digits[0] == 36){ // Carryover to tens digit digits[0] = 0; digits[1]++; } if (digits[1] == 36){ // Carryover to hundreds digit digits[1] = 0; digits[2]++; } if (digits[2] == 36){ // Carryover to thousands digit digits[2] = 0; digits[3]++; } // Always convert the ones digit to a character potentialPassword[0] = convertDigitToCharacter(digits[0]); // Only convert the tens digit to a character once a carryover to the tens digit has occurred if (i >= 36){ if (i == 36) digits[1] = 0; // Must initialize to 0, otherwise the carryover will increment // and just start at 1 potentialPassword[1] = convertDigitToCharacter(digits[1]); } // Only convert the hundreds digit to a character once a carryover to the hundreds digit has occurred if (i >= 1296){ if (i == 1296) digits[2] = 0; // Must initialize to 0, otherwise the carryover will increment // and just start at 1 potentialPassword[2] = convertDigitToCharacter(digits[2]); } // Only convert the thousands digit to a character once a carryover to the thousands digit has occurred if (i >= 46656){ if (i == 46656) digits[3] = 0; // Must initialize to 0, otherwise the carryover will increment // and just start at 1 potentialPassword[3] = convertDigitToCharacter(digits[3]); } digits[0]++; // Increment the ones digit here rather than earlier so [0000] is checked as a possible solution hashWord(potentialPassword, hash, hex_str); // Convert the permutation into hex if (unsolvedPassword->hex_str == hex_str){ // Check if the hex matches the unsolved password hex // If it does, store everything! unsolvedPassword->plainTextSolution = potentialPassword; std::pair<int, SolvedEntry*> newPair(unsolvedPassword->passwordEntryNumber, unsolvedPassword); solvedPasswords.insert(newPair); return; // Break out of this function because the password has been decrypted, don't need to try other permutations } } // Password was not able to be decrypted unsolvedPassword->plainTextSolution = "????"; std::pair<int, SolvedEntry*> newPair(unsolvedPassword->passwordEntryNumber, unsolvedPassword); solvedPasswords.insert(newPair); }
int Oligos::readOligos(){ try { ifstream inOligos; m->openInputFile(oligosfile, inOligos); string type, oligo, roligo, group; bool pfUsesNone = false; bool prUsesNone = false; bool bfUsesNone = false; bool brUsesNone = false; while(!inOligos.eof()){ inOligos >> type; if (m->debug) { m->mothurOut("[DEBUG]: reading type - " + type + ".\n"); } if(type[0] == '#'){ while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there m->gobble(inOligos); } else{ m->gobble(inOligos); //make type case insensitive for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } inOligos >> oligo; if (m->debug) { m->mothurOut("[DEBUG]: reading - " + oligo + ".\n"); } for(int i=0;i<oligo.length();i++){ oligo[i] = toupper(oligo[i]); if(oligo[i] == 'U') { oligo[i] = 'T'; } } if(type == "FORWARD"){ group = ""; // get rest of line in case there is a primer name while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13 || c == -1){ break; } else if (c == 32 || c == 9){;} //space or tab else { group += c; } } //check for repeat barcodes map<string, int>::iterator itPrime = primers.find(oligo); if (itPrime != primers.end()) { m->mothurOut("[WARNING]: primer " + oligo + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); } else { if (m->debug) { if (group != "") { m->mothurOut("[DEBUG]: reading group " + group + ".\n"); }else{ m->mothurOut("[DEBUG]: no group for primer " + oligo + ".\n"); } } primers[oligo]=indexPrimer; indexPrimer++; primerNameVector.push_back(group); } } else if (type == "PRIMER"){ m->gobble(inOligos); inOligos >> roligo; for(int i=0;i<roligo.length();i++){ roligo[i] = toupper(roligo[i]); if(roligo[i] == 'U') { roligo[i] = 'T'; } } if (oligo == "NONE") { pfUsesNone = true; } else if (roligo == "NONE") { prUsesNone = true; } if (roligo != "NONE") { if (reversePairs) { roligo = reverseOligo(roligo); } } group = ""; // get rest of line in case there is a primer name while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13 || c == -1){ break; } else if (c == 32 || c == 9){;} //space or tab else { group += c; } } oligosPair newPrimer(oligo, roligo); if (m->debug) { m->mothurOut("[DEBUG]: primer pair " + newPrimer.forward + " " + newPrimer.reverse + ", and group = " + group + ".\n"); } //check for repeat barcodes string tempPair = oligo+roligo; if (uniquePrimers.count(tempPair) != 0) { m->mothurOut("primer pair " + newPrimer.forward + " " + newPrimer.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); } else { uniquePrimers.insert(tempPair); if (m->debug) { if (group != "") { m->mothurOut("[DEBUG]: reading group " + group + ".\n"); }else{ m->mothurOut("[DEBUG]: no group for primer pair " + newPrimer.forward + " " + newPrimer.reverse + ".\n"); } } pairedPrimers[indexPairedPrimer]=newPrimer; indexPairedPrimer++; primerNameVector.push_back(group); hasPPrimers = true; } } else if(type == "REVERSE"){ string oligoRC = reverseOligo(oligo); revPrimer.push_back(oligoRC); } else if(type == "BARCODE"){ inOligos >> group; //barcode lines can look like BARCODE atgcatgc groupName - for 454 seqs //or BARCODE atgcatgc atgcatgc groupName - for illumina data that has forward and reverse info string temp = ""; while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13 || c == -1){ break; } else if (c == 32 || c == 9){;} //space or tab else { temp += c; } } //then this is illumina data with 4 columns if (temp != "") { hasPBarcodes = true; string reverseBarcode = group; //reverseOligo(group); //reverse barcode group = temp; for(int i=0;i<reverseBarcode.length();i++){ reverseBarcode[i] = toupper(reverseBarcode[i]); if(reverseBarcode[i] == 'U') { reverseBarcode[i] = 'T'; } } if (oligo == "NONE") { bfUsesNone = true; } else if (reverseBarcode == "NONE") { brUsesNone = true; } if (reverseBarcode != "NONE") { if (reversePairs) { reverseBarcode = reverseOligo(reverseBarcode); } } oligosPair newPair(oligo, reverseBarcode); if (m->debug) { m->mothurOut("[DEBUG]: barcode pair " + newPair.forward + " " + newPair.reverse + ", and group = " + group + ".\n"); } //check for repeat barcodes string tempPair = oligo+reverseBarcode; if (uniqueBarcodes.count(tempPair) != 0) { m->mothurOut("barcode pair " + newPair.forward + " " + newPair.reverse + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); } else { uniqueBarcodes.insert(tempPair); pairedBarcodes[indexPairedBarcode]=newPair; indexPairedBarcode++; barcodeNameVector.push_back(group); } }else { //check for repeat barcodes map<string, int>::iterator itBar = barcodes.find(oligo); if (itBar != barcodes.end()) { m->mothurOut("[WARNING]: barcode " + oligo + " is in your oligos file already, disregarding."); m->mothurOutEndLine(); } else { barcodes[oligo]=indexBarcode; indexBarcode++; barcodeNameVector.push_back(group); } } }else if(type == "LINKER"){ linker.push_back(oligo); }else if(type == "SPACER"){ spacer.push_back(oligo); } else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); } }
void GameObjectManager::AddObject(uint id, GameObject* object){ std::pair<uint, GameObject*> newPair(id, object); objects.insert(newPair); }
bool SMatrix::setVal(size_type row, size_type col, int val) throw(MatrixError) { bool valSet = false; if(row > m_ && col > n_) { throw MatrixError("Matrix bound error"); //CHANGE LATER?? } else { if(ridx_.find(row) != ridx_.cend()) { //row already in ridx int nElements = ridx_[row].first + ridx_[row].second; //cout << ridx_[row].first << " " << ridx_[row].second << endl; //finding insert position int pos; for(pos = ridx_[row].first;pos < nElements;++pos) { if(col < cidx_[pos]) { break; } } //if at near capacity double the size of the arrays if(k_ >= arrSize_) { //arrSize_ - 2) { valSet = true; //returns true if additional memory must be allocated int* tempVal = vals_; size_type* tempCidx = cidx_; arrSize_ = arrSize_*2; vals_ = new int[arrSize_]; cidx_ = new size_type[arrSize_]; for(size_type i = 0;i < k_;++i) { vals_[i] = tempVal[i]; cidx_[i] = tempCidx[i]; } } //loop to find insert position for(int i = k_; i > pos;--i) { vals_[i] = vals_[i-1]; cidx_[i] = cidx_[i-1]; } vals_[pos] = val; //inserting new element cidx_[pos] = col; ++k_; ++ridx_[row].second; //cout << "row: " << row << "num elements" << ridx_[row].second << endl; //cout << vals_[0] << " " << vals_[1] << " " << vals_[2] <<endl; //cout << cidx_[0] << " " << cidx_[1] << " " << cidx_[2] << endl; //cout << ridx_[row].first << " " << ridx_[row].second << endl; } else { if(val != 0) { //avoid creating new entry if it is a non zero element if(k_ > arrSize_) { valSet = true; //returns true if additional memory must be allocated //allocate more memory } else { vals_[k_] = val; //setting val cidx_[k_] = col; //setting corresponding column pair<size_t, unsigned int> newPair(k_,1); //new pair to add to ridx ridx_[row] = newPair; ++k_; } } } } return valSet; }
void graph<vertex_type>::insert(const vertex_type & item, const list< pair<vertex_type, double> > & adjList) { typename unordered_map<vertex_type, vertex<vertex_type>* >::const_iterator lookup = graph_.find(item); if( lookup == graph_.end()) { vertex<vertex_type>* newVertex = new vertex<vertex_type>(item); for(auto list_entry: adjList) { lookup = graph_.find(list_entry.first); if(lookup == graph_.end()) { vertex<vertex_type>* temp_vertex = new vertex<vertex_type>{list_entry.first}; pair< vertex_type, vertex<vertex_type>* > pairToInsert(list_entry.first, temp_vertex); graph_.insert(pairToInsert); newVertex->addToList(temp_vertex, list_entry.second); } else//vertex found in graph { newVertex->addToList(lookup->second, list_entry.second); } } pair<vertex_type, vertex<vertex_type>* > newPair(item, newVertex); graph_.insert(newPair); } else //item is already a vertex in the graph { vertex<vertex_type>* current_vertex = lookup->second; for(auto list_entry: adjList) { lookup = graph_.find(list_entry.first); if(lookup == graph_.end())//not found { vertex<vertex_type>* temp_vertex = new vertex<vertex_type>{list_entry.first}; pair< vertex_type, vertex<vertex_type>* > pairToInsert(list_entry.first, temp_vertex); graph_.insert(pairToInsert); current_vertex->addToList(temp_vertex, list_entry.second); } else//vertex found in graph { current_vertex->addToList(lookup->second, list_entry.second); } } } }
quad get_tr(node *t){ if(t == NULL) return newQuad(newPair(1,0), newPair(0,1)); return t->tr; }
quad mkQuad(ll v){ quad rv; rv.first = newPair(1,(v-1)/2); rv.second = newPair(1, v/2 ); return rv; }