void gpHelper(int l, int r, string &sol, vector<string> &res){ if(l==0 && r==0){ res.push_back(sol); return; } if(l>0){ sol.push_back('('); gpHelper(l-1, r, sol, res); sol.pop_back(); } if(r>l){ sol.push_back(')'); gpHelper(l, r-1, sol, res); sol.pop_back(); } }
void binary(int number, string & numbuf) { int remainder; if(number == 0) { numbuf.push_back('0'); return; } if(number == 1) { numbuf.push_back('1'); return; } remainder = number%2; binary(number >> 1, numbuf); if(remainder==0){numbuf.push_back('0');} if(remainder==1){numbuf.push_back('1');} return; }
string add(string num1, string num2) { // 对两个string相加 reverse(num1.begin(),num1.end()); reverse(num2.begin(),num2.end()); if(num1.size() < num2.size()) num1.swap(num2); int cflag = 0; int i = 0; for(; i < num2.size(); ++i) { int temp = (num2[i] - '0')+(num1[i]-'0')+cflag; cflag = temp/10; num1[i] = temp%10 + '0'; } while(cflag&&i<num1.size()) { int temp = num1[i]-'0'+cflag; cflag = temp/10; num1[i++] = temp%10 + '0'; } if(cflag) num1.push_back(cflag+'0'); reverse(num1.begin(),num1.end()); return num1; }
inline static void ullcopyreverse(const char *rbuf, string& buf, int idx) { buf.reserve(idx + 1); for (int i = idx - 1; i >= 0; i--) { buf.push_back(rbuf[i]); } }
void traverse( const vector<vector<char>> &board, vector<vector<bool>> &visited, int i, int j, int m, int n, const shared_ptr<TrieNode> ¤t, string &word, unordered_set<string> &result) { if (i<0 || i>=m || j<0 || j>=n || visited[i][j] || current == nullptr) { return; } auto it = current->next.find(board[i][j]); if (it != current->next.end()) { auto next = it->second; visited[i][j] = true; word.push_back(board[i][j]); if (next->end) { result.insert(word); } traverse(board, visited, i+1, j, m, n, next, word, result); traverse(board, visited, i-1, j, m, n, next, word, result); traverse(board, visited, i, j+1, m, n, next, word, result); traverse(board, visited, i, j-1, m, n, next, word, result); word.pop_back(); visited[i][j] = false; } }
bool CreateComtrade::create(string filepath, string filename) { if( filepath.empty() || filename.empty()) return false; if( filepath[filepath.size() - 1] != '/' ) { filepath.push_back('/'); } clacTimes(); string cfgname = filepath; cfgname += filename; cfgname += ".cfg"; createCFG( cfgname ); string datname = filepath; datname += filename; datname += ".dat"; if( m_comtradeHead.datType.compare("ASCII") == 0 || m_comtradeHead.datType.compare("ascii")==0){ createAsciiDAT( datname ); }else{ createBinaryDAT( datname ); } }
string reverse(string a, string b) { int i; for (i = 0; i<a.size(); i++){ b.push_back(a[a.size() - i - 1]); } return b; }
string convert(string s, int nRows) { if(nRows<2) return s; int hip = 2*nRows-2; int n = s.size()/hip; for(int i = 0; i <nRows; i++){ for(int j = 0; j<= n; j++){ if(j*hip+i<s.size()) ans.push_back(s[j * hip + i]); if(i == 0 || i == nRows -1) continue; else if(j*hip+hip-i<s.size()) ans.push_back(s[j * hip + hip-i]); } } return ans; }
bool _STLP_CALL __copy_grouped_digits(_InputIter& __first, _InputIter& __last, string& __v, const _CharT * __digits, _CharT __sep, const string& __grouping, bool& __grouping_ok) { bool __ok = false; char __group_sizes[64]; char*__group_sizes_end = __group_sizes; char __current_group_size = 0; for ( ; __first != __last; ++__first) { _CharT __c = *__first; bool __tmp = __get_fdigit_or_sep(__c, __sep, __digits); if (__tmp) { if (__c == ',') { *__group_sizes_end++ = __current_group_size; __current_group_size = 0; } else { __ok = true; __v.push_back((char)__c); ++__current_group_size; } } else break; } if (__group_sizes_end != __group_sizes) *__group_sizes_end++ = __current_group_size; __grouping_ok = __valid_grouping(__group_sizes, __group_sizes_end, __grouping.data(), __grouping.data() + __grouping.size()); return __ok; }
void GenerateSuffixList() { string buffer; int c=0; map<string,int>::iterator it; // collect input while (cin >> buffer) { Input.append(buffer); } // append sentinel character Input.push_back('$'); // DEBUG // cout << Input << endl; // generate substrings from current the position to the end while (c < Input.size()) { buffer = Input.substr(c, (Input.size()-c)); SortedSuffixList[buffer] = c; c++; // cout << buffer << endl; } // cout << "check" << endl; // generate suffix array for (it=SortedSuffixList.begin();it!=SortedSuffixList.end();it++) { // cout << "check2" << endl; SuffixArray.push_back(it->second); // DEBUG // cout << it->first << ": " << it->second << endl; } SortedSuffixList.clear(); }
void constructSA() { //T[n++] = '$'; T.push_back('$'); n++; for(int i = 0; i < n; i++) RA[i] = T[i] - '$'; for(int i = 0; i < n; i++) SA[i] = i; int rank; for(int k = 1; k < n; k <<= 1) { countingSort(k); countingSort(0); tempRA[SA[0]] = rank = 0; for(int i = 1; i < n; i++) { if(RA[SA[i]] == RA[SA[i-1]] && RA[SA[i] + k] == RA[SA[i-1] + k]) { tempRA[SA[i]] = rank; } else { rank++; tempRA[SA[i]] = rank; } } for(int i = 0; i < n; i++) RA[i] = tempRA[i]; } }
string rotate( string s ) { char c = s[ 0 ]; s.erase( 0, 1 ); s.push_back( c ); return s; }
int MemoryBuilder::getString(const uint32_t add, string& str, bool bAppend) { if (!bAppend) { str.clear(); } uint32_t offset = (add & ~PAGE_MASK); //first lets find the right page map< uint32_t, RebuiltPage* >::iterator it; it = memory.find(ADDR_TO_KEY(add)); if (it == memory.end()) { return (-1); } RebuiltPage* pTemp = (*it).second; char* src = (char*)(pTemp->page); size_t i = offset; while ((i < PAGE_SIZE) && (src[i] != '\0') && (pTemp->bitmap[i])) { str.push_back(src[i]); i++; } //now check to see if we are at a page boundary, if we are then we need to continue if (i == PAGE_SIZE) { getString((add & PAGE_MASK) + (0x1 << PAGE_SHIFT), str, true); } return (0); }
int calculate(string s) { stack<char> optr; stack<int> opnd; optr.push('#'); s.push_back('#'); int i = 0; while (i < s.length()) { if (s[i] == ' ') { i++; continue; } if (isdigit(s[i])) { int num = 0; while (isdigit(s[i])) num = 10 * num + s[i++] - '0'; opnd.push(num); continue; } switch(precede(optr.top(), s[i])) { case '<': optr.push(s[i++]); break; case '=': optr.pop(); i++; break; case '>': char theta = optr.top(); optr.pop(); int b = opnd.top(); opnd.pop(); int a = opnd.top(); opnd.pop(); int c = operate(a, theta, b); opnd.push(c); break; } } return opnd.top(); }
void findMaxCommonSubseq (const string& a, const string& b, string& res) { vector<vector<int> > ans (a.size () + 1, vector<int> (b.size () + 1, 0)); for (size_t i = 0; i < a.size (); ++i) for (size_t j = 0; j < b.size (); ++j) { if (a[i] == a[j]) ans[i + 1][j + 1] = ans[i][j] + 1; else ans[i + 1][j + 1] = max (ans[i][j + 1], ans[i + 1][j]); } size_t i = a.size (), j = b.size (); while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { res.push_back (a[i - 1]); --i; --j; } else if (ans[i - 1][j] > ans[i][j - 1]) --i; else --j; } }
void topologicalSort(char ch) { visited[ch] = true; topoSort.push_back(ch); for(auto& c : adjList[ch]) { --indegree[c]; } char c; for(auto& pr : adjList) { c = pr.first; if(!indegree[c] && !visited[c]) { topologicalSort(c); } } if(topoSort.length() == n) { cout << topoSort << '\n'; } visited[ch] = false; topoSort.pop_back(); for(auto& c : adjList[ch]) { ++indegree[c]; } }
void generateParenthesis(int n, int left, int right, string& s) { if (left == n && right == n) { r.push_back(s); return; } if (right < left) { s.push_back(')'); generateParenthesis(n, left, right + 1, s); s.pop_back(); } if (left < n) { s.push_back('('); generateParenthesis(n, left + 1, right, s); s.pop_back(); } }
void incr(string &c, int &csz, int pos) { int carry=1,sum=0; int i; for(i=pos;i<csz;++i) { sum=c[i]-'0'+carry; if(sum==2) { sum=0; carry=1; c[i]='0'; } else { c[i]=sum+'0'; carry=0; break; } } if(i==csz && carry==1) { csz+=1; c.push_back('1'); } }
int main() { #ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin); // freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout); #endif ios_base::sync_with_stdio(0); while(cin >> n >> s) { s.push_back('_'); bool out = true; int cnt = 0; int maxv = 0, inCnt = 0; for(char c : s) { if(!isalpha(c)) { if(out) maxv = max(maxv, cnt); else if(cnt) ++inCnt; // pr(c); pr(out); pr(cnt); prln(inCnt); if(c == '(') out = false; else if(c == ')') out = true; cnt = 0; } else { ++cnt; } } cout << maxv << ' ' << inCnt << endl; } return 0; }
void HTTPInputStream::ReadHeaderLine(string & line) { bool gotCR = false; while (true) { char c; int nread = read(sockfd, &c, 1); if (nread == 1) { if (gotCR) { if (c == '\n') { return; } else { throw NetworkException("invalid HTTP header"); } } else if (c == '\r') { gotCR = true; } else if (c == '\n') { throw NetworkException("invalid HTTP header"); } else { line.push_back(c); } } else { throw NetworkException("invalid HTTP header"); } } }
void preorder(int u, const vector<PII>& children, string& ans) { ans.push_back(mb.at(u)); int v = children[u].first; int w = children[u].second; if (v >= 0) preorder(v, children, ans); if (w >= 0) preorder(w, children, ans); }
void WordSearchII::findWords(const vector<vector<char>> &board, vector<vector<bool>> &visited, int row, int col, string word, set<string> &result) { int nrows = board.size(); int ncols = board[0].size(); if (row < 0 || row >= nrows || col < 0 || col >= ncols) return; if (visited[row][col]) return; word.push_back(board[row][col]); if (!trie.startsWith(word)) return; if (trie.search(word)) result.insert(word); visited[row][col] = true; findWords(board, visited, row + 1, col, word, result); findWords(board, visited, row - 1, col, word, result); findWords(board, visited, row, col + 1, word, result); findWords(board, visited, row, col - 1, word, result); visited[row][col] = false; }
void search(const string &s, int cur, int num, vector<string> &res, string &path){ if(s.length()-cur<num || s.length()-cur>3*num) return; if(cur==s.length() && num==0){ path.pop_back(); res.push_back(path); path.push_back('.'); return; } // 1 digit path+=s.substr(cur, 1)+"."; cur+=1; --num; search(s, cur, num, res, path); cur-=1; ++num; path.erase(path.length()-2, 2); // 2 digits if(cur+1<s.length() && s[cur]!='0'){ path+=s.substr(cur, 2)+"."; cur+=2; --num; search(s, cur, num, res, path); cur-=2; ++num; path.erase(path.length()-3, 3); } // 3 digits if(cur+2<s.length()){ string sub=s.substr(cur, 3); int x=stoi(sub); if(x<100 || x>255) return; path+=sub+"."; cur+=3; --num; search(s, cur, num, res, path); cur-=3; ++num; path.erase(path.length()-4, 4); } }
// consume up to the given string, if not foudn return 0 else bytes consumed int IOBuffer::consumeUntil(string until, string& found) { int search_size = until.size(); const char* search = until.c_str(); int k = 0; for(int i = consumed; i < published; ++i) { found.push_back((char)buffer[i]); if(buffer[i] == search[0]) { // out of buffer. if( (i + search_size) > published) { return 0; } // check if next couple of chars is the search string. for(k = 0; k < search_size; ++k) { if(buffer[i+k] != search[k]) { break; } } // we found the search string, update consumed! if(k == search_size) { found.append((char*)buffer+(i+1), search_size-1); consumed += (i+search_size); return consumed; } } } found = ""; return 0; }
void generateParenthesisHelper(int l, int r, string &sol, vector<string> &res) { if (r == 0) { res.push_back(sol); return; } if (l > 0) { sol.push_back('('); generateParenthesisHelper(l - 1, r, sol, res); sol.pop_back(); } if (r > l) { sol.push_back(')'); generateParenthesisHelper(l, r - 1, sol, res); sol.pop_back(); } }
void compute(string str,string newStr, stack<char> s,int len,int curr) { if(newStr.size()==len) { if(mymap[newStr]==0) { mymap[newStr]=1; dCnt++; } res.push_back(newStr); if(orig==newStr) cnt++; } else { //cout<<s.size()<<" size "<<endl; if(!s.empty()) { char popped; popped = s.top(); //cout<<s.top()<<" top "<<endl; newStr.push_back(popped); s.pop(); compute(str,newStr,s,len,curr); s.push(popped); newStr = newStr.substr(0,newStr.size()-1); } if(str.size()>0) { s.push(str.at(0)); str = str.substr(1,str.size()-1); //cout<<str<<" hello "<<endl; curr++; compute(str,newStr,s,len,curr); } } }
void help(vector<string> & r, string s, int idx, int part, string & v){ if(4 == part && idx == s.size()){ r.push_back(v); return; } int rma = (3 - part) * 3; int rmi = (3 - part) * 1; size_t old = v.size(); for(int i = 1;i <= 3;++i){ int left = s.size() - idx - i; if(rmi <= left && left <= rma){ if(3 == i){ int v = 100 * (s[idx] - '0') + 10 * (s[idx + 1] - '0') + s[idx + 2] - '0'; if(v > 255) continue; } v.resize(old); v.append(s, idx, i); if(part < 3) v.push_back('.'); help(r, s, idx + i, part + 1, v); } if('0' == s[idx]) break; } }
void to_upper_case(string &res, const char *src) { while (*src) { res.push_back(toupper(*src ++)); } }
void join_str(string& s, long long num, int t) { if (!num) { if (t < 0) s.push_back('-'); s.push_back('0'); return; } while (num) { s.push_back(num % 10 + '0'); num /= 10; } if (t < 0) { s.push_back('-'); } reverse(s.begin(),s.end()); }
void DatabaseClient::appendSetSql(string& sql, DynamicObject& params) { bool first = true; DynamicObjectIterator i = params.getIterator(); while(i->hasNext()) { DynamicObject& param = i->next(); if(first) { first = false; sql.append(" "); } else { sql.append(","); } // append name if(param->hasMember("tableAlias")) { sql.append(param["tableAlias"]->getString()); sql.push_back('.'); } sql.append(param["name"]->getString()); sql.append("=?"); } }