string possible(vector <string> score, vector <int> result) { lev = score; vpii vec; int n = sz(score); int m = sz(score[0]); rep(i,n-1) vec.pb( pii(result[i],result[i+1]) ); vector<bool> used(m,0); while(true){ bool ent = false; rep(j,m){ if(used[j]) continue; if(!canTake(vec,j)) continue; used[j]=true; ent = true; stack<int> st; rep(i,sz(vec)) { if(score[vec[i].xx][j] < score[vec[i].yy][j]) st.push(i); } while(!st.empty()){ vec.erase( vec.begin()+st.top() ); st.pop(); } } if(!ent) break; } rep(i,sz(vec)) if(vec[i].xx>vec[i].yy) return "Impossible"; return "Possible"; }
int longestValidParentheses(string s) { stack<char> s; int len = s.length(); if(len == 0) { return 0; } int now = 0; int ret = 0; int lasi = 0; int i = 0; while(i < len) { if(s[i] == '(') { s.push(i); } else { if(s.empty()) { lasti = i+1; } else { s.pop(); if(s.empty()) { ret = max(ret,i-lasti+1); } else { now = s.top(); ret = max(ret,i-now); } } } i ++; } return ret; }
string traverse_in_order(Vertex* r) { string out = ""; if (r == NULL) return out; stack<Vertex*> s; Vertex* p = root; while (p != NULL) { s.push(p); p = p->left; } while (!s.empty()) { p = s.top(); out.push_back(p->key); s.pop(); p = p->right; while (p != NULL) { s.push(p); p = p->left; } } return out; }
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> > ret(0); int i = 0 , se = 0, len = start.length(), runlen = 0; unordered_map<string, int> m[2] ; unordered_map<string, string> parent ; //unordered_set<char> alpha({'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}); queue<string> q[2] ; string str[2] ; str[0] = end ; str[1] = start ; m[1][start] = 1 ; m[0][end] = 1 ; q[1].push(start) ; q[0].push(end) ; while(1){ string ref ; se = ! se ; if(q[se].empty()){ break; } ref = q[se].front() ; for(i = 0; i < len; i++){ char c ; //unordered_set<char> a (alpha) ; //a.erase(ref[i]) ; for( c = 'a'; c <= 'z'; c++){ if(c == ref[i]){continue;} string temp (ref) ; temp[i] = c ; if(m[!se][temp] > 0){ int newrunlen = m[se][q[se].front()] + m[!se][temp] ; if(newrunlen == runlen || runlen == 0 ){ vector<string> transformation ; stack<string> start ; queue<string> end ; runlen = newrunlen ; string str (q[se].front()) ; while(str.length() == len ){ if(se == 1){ start.push(str) ; }else{ end.push(str); } str = parent[str] ; } str = temp ; while(str.length() == len){ if(se == 1){ end.push(str) ; }else{ start.push(str) ; } str = parent[str] ; } while(!start.empty()){ transformation.push_back(start.top()) ; start.pop() ; } while(!end.empty()){ transformation.push_back(end.front()) ; end.pop() ; } ret.push_back(transformation); }else{ return ret ; } } if(dict.find(temp) != dict.end()){ dict.erase(temp); q[se].push(temp) ; m[se][temp] = m[se][q[se].front()] + 1; parent[temp] = q[se].front() ; } } } m[se][q[se].front()] = 0 ; q[se].pop(); } return ret ; }