//static void dfs_insert(lts_t inv,int *map,int *newmap,int label,int dest,int state){ static void dfs_insert(int state){ int set,j,last,mymap,pred; set=SetInsert(newmap[state],label,dest); if (set != newmap[state]) { newmap[state]=set; last=inv->begin[state+1]; mymap=map[state]; for(j=inv->begin[state];j<last;j++){ pred=inv->dest[j]; if (mymap==map[pred]){ //dfs_insert(inv,map,newmap,label,dest,pred); dfs_insert(pred); } } } }
void dfs_insert(vector<vector<string>> &res, deque<string> &strpath, unordered_map<string, vector<string>> &path, string & key) { vector<string> before = path[key]; int len = before.size(); for(int i=0; i<len; i++) { string now_key = before[i]; if(now_key.empty()) { vector<string> temp(strpath.begin(), strpath.end()); res.push_back(temp); } else { strpath.push_front(now_key); dfs_insert(res, strpath, path, now_key); strpath.pop_front(); } } }
void set_reduce_branching2(lts_t lts){ int tau,count,i,*tmp,iter,s,l,d,setcount,j,set,*repr,t_count,tag,num_states,last_trans; //int *map,*newmap; //lts_t inv; tau=lts->tau; lts_divergence_marking(lts); if (tau_cycle_elim) { lts_tau_cycle_elim(lts); Warning(1,"size after tau cycle elimination is %d states and %d transitions",lts->states,lts->transitions); } if (tau_indir_elim) { lts_tau_indir_elim(lts); Warning(1,"size after trivial tau elimination is %d states and %d transitions",lts->states,lts->transitions); } num_states=lts->states; map=(int*)malloc(sizeof(int)*lts->states); newmap=(int*)malloc(sizeof(int)*lts->states); lts_set_type(lts,LTS_LIST); inv=lts_create(); lts_set_type(inv,LTS_LIST); lts_set_size(inv,lts->states,lts->transitions); inv->root=0; count=0; for(i=0;i<lts->transitions;i++) if (lts->label[i]==tau){ inv->src[count]=lts->dest[i]; inv->label[count]=lts->label[i]; inv->dest[count]=lts->src[i]; count++; } lts_set_size(inv,lts->states,count); lts_sort(lts); lts_set_type(lts,LTS_BLOCK); lts_set_type(inv,LTS_BLOCK); for(i=0;i<num_states;i++){ map[i]=0; newmap[i]=EMPTY_SET; } count=1; iter=0; for(;;){ SetClear(-1); iter++; for(i=0;i<num_states;i++){ last_trans=lts->begin[i+1]; for(j=lts->begin[i];j<last_trans;j++){ label=lts->label[j]; dest=map[lts->dest[j]]; if (label!=tau || map[i]!=dest) { //dfs_insert(inv,map,newmap,lts->label[j],map[lts->dest[j]],i); dfs_insert(i); } } } SetSetTag(newmap[lts->root],0); setcount=1; for(i=0;i<num_states;i++){ set=newmap[i]; if (SetGetTag(set)<0) { //fprintf(stderr,"new set:"); //PrintSet(stderr,set); //fprintf(stderr,"\n"); SetSetTag(set,setcount); setcount++; } } Warning(2,"count is %d",setcount); if(count==setcount) break; count=setcount; for(i=0;i<num_states;i++){ map[i]=SetGetTag(newmap[i]); newmap[i]=EMPTY_SET; } } repr=(int*)malloc(sizeof(int)*count); for(i=0;i<count;i++) { repr[i]=-1; } t_count=0; for(i=0;i<lts->states;i++){ if(repr[map[i]]==-1){ repr[map[i]]=i; t_count+=SetGetSize(newmap[i]); } } lts_set_size(lts,count,t_count); lts->root=0; lts->root2=map[lts->root2]; lts->begin[0]=0; for(i=0;i<lts->states;i++){ count=lts->begin[i]; set=newmap[repr[i]]; while(set!=EMPTY_SET){ lts->label[count]=SetGetLabel(set); lts->dest[count]=SetGetDest(set); set=SetGetParent(set); count++; } lts->begin[i+1]=count; } SetFree(); free(newmap); free(map); free(repr); Warning(1,"set2 reduction took %d iterations",iter); }
vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<string>> res; vector<string> temp; if(differ(beginWord, endWord)<=1) { temp.push_back(beginWord); temp.push_back(endWord); res.push_back(temp); return res; } unordered_map<string, vector<string>> path; unordered_set<string> last_word; unordered_set<string> last_use; bool flag = false; string none; path[beginWord].push_back(none); queue<pair<string, int>> q; q.push(pair<string, int> (beginWord, 1)); wordList.erase(beginWord); string str, nxt; int cntStep; int lastStep =0; int minStep; while (!q.empty()) { str = q.front().first; cntStep = q.front().second; if(flag && cntStep >= minStep) break; if(lastStep != cntStep) { lastStep =cntStep; for(auto it = last_use.begin(); it != last_use.end(); it++) wordList.erase(*it); last_use.clear(); } q.pop(); for (int i = 0; i < str.length(); i++) for (char j = 'a'; j <= 'z'; j++) { if (str[i] == j) continue; nxt = str; nxt[i] = j; if(nxt == endWord) { flag = true; minStep = cntStep+1; if(last_word.count(str)) break; last_word.insert(str); deque<string> strpath; strpath.push_front(endWord); strpath.push_front(str); dfs_insert(res, strpath, path, str); } else if (wordList.count(nxt)) { last_use.insert(nxt); path[nxt].push_back(str); q.push(pair<string, int> (nxt, cntStep+1)); } } } return res; }