//prev map is excellent vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> > result; vector<string> ret; unordered_map<string, unordered_set<string> > prevMap; vector<unordered_set<string> > candidates(2); candidates[0].insert(start); while (true) { /* for (auto it = candidates[0].begin(); it != candidates[0].end(); it ++) {*/ //cout << *it << " "; //} //cout << endl; //for (auto it = dict.begin(); it != dict.end(); it ++) { //cout << *it << " "; //} /*cout << endl;*/ for (auto it = candidates[0].begin(); it != candidates[0].end(); it ++) { dict.erase(*it); } candidates[1].clear(); for (auto it = candidates[0].begin(); it != candidates[0].end(); it ++) { for (int i = 0; i < (int) it->length(); i ++) { string word = *it; for (char j = 'a'; j <= 'z'; j ++) { if (word[i] == j) { continue; } word[i] = j; if (dict.find(word) != dict.end()) { prevMap[word].insert(*it); candidates[1].insert(word); } if (word == end) { prevMap[word].insert(*it); candidates[1].insert(word); } } } } if (candidates[1].empty()) { return result; } if (candidates[1].count(end)) { break; } candidates[0].swap(candidates[1]); } cout << "f**k" << endl; generateResult(prevMap, ret, end, result); return result; }
void generateResult(unordered_map<string, unordered_set<string> > &prevMap, vector<string> &ret, const string &word, vector<vector<string> > &result) { if (prevMap[word].size() == 0) { ret.push_back(word); vector<string> path = ret; reverse(path.begin(), path.end()); result.push_back(path); ret.pop_back(); return ; } ret.push_back(word); for (auto it = prevMap[word].begin(); it != prevMap[word].end(); it ++) { generateResult(prevMap, ret, *it, result); } ret.pop_back(); }
// PD_TRACE_DECLARE_FUNCTION ( COORD_CMDSTATBASE_EXE, "_coordCMDStatisticsBase::execute" ) INT32 _coordCMDStatisticsBase::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( COORD_CMDSTATBASE_EXE ) ; SDB_RTNCB *pRtncb = pmdGetKRCB()->getRTNCB() ; contextID = -1 ; coordQueryOperator queryOpr( isReadOnly() ) ; rtnContextCoord *pContext = NULL ; coordQueryConf queryConf ; coordSendOptions sendOpt ; queryConf._openEmptyContext = openEmptyContext() ; CHAR *pHint = NULL ; rc = msgExtractQuery( (CHAR*)pMsg, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &pHint ); PD_RC_CHECK ( rc, PDERROR, "Execute failed, failed to parse query " "request, rc: %d", rc ) ; try { BSONObj boHint( pHint ) ; BSONElement ele = boHint.getField( FIELD_NAME_COLLECTION ) ; PD_CHECK ( ele.type() == String, SDB_INVALIDARG, error, PDERROR, "Execute failed, failed to get the field(%s)", FIELD_NAME_COLLECTION ) ; queryConf._realCLName = ele.str() ; } catch( std::exception &e ) { PD_RC_CHECK ( rc, PDERROR, "Execute failed, occured unexpected " "error:%s", e.what() ) ; } rc = queryOpr.init( _pResource, cb, getTimeout() ) ; if ( rc ) { PD_LOG( PDERROR, "Init query operator failed, rc: %d", rc ) ; goto error ; } rc = queryOpr.queryOrDoOnCL( pMsg, cb, &pContext, sendOpt, &queryConf, buf ) ; PD_RC_CHECK( rc, PDERROR, "Query failed, rc: %d", rc ) ; rc = generateResult( pContext, cb ) ; PD_RC_CHECK( rc, PDERROR, "Failed to execute statistics, rc: %d", rc ) ; contextID = pContext->contextID() ; pContext->reopen() ; done: PD_TRACE_EXITRC ( COORD_CMDSTATBASE_EXE, rc ) ; return rc; error: if ( pContext ) { pRtncb->contextDelete( pContext->contextID(), cb ) ; } goto done ; }