void push(int x) { if (minStack.empty() || x <= minStack.top()) minStack.push(x); mainStack.push(x); }
int main() { int t; LL a,b,k; scanf("%d", &t); while(t--) { scanf("%s", str); int len = strlen(str); for(int i = 0; i < len; i++) { if(digit(str[i])) { k = str[i] - '0'; i++; while(digit(str[i])) { k = k*10 + str[i] - '0'; i++; } i--; num.push(k); } else if(str[i] == '+') { if(!op.empty()) { while(op.top() == '*' || op.top() == '/') { char c = op.top(); op.pop(); a = num.top(); num.pop(); b = num.top(); num.pop(); if(c == '*') { num.push(a*b); } else if(c == '/'){ num.push(b/a); } } } op.push(str[i]); } else if(str[i] == '-') { if(!op.empty()) { while(op.top() == '*' || op.top() == '/') { char c = op.top(); op.pop(); a = num.top(); num.pop(); b = num.top(); num.pop(); if(c == '*') { num.push(a*b); } else if(c == '/'){ num.push(b/a); } } } op.push(str[i]); } else if(str[i] == '*') { op.push(str[i]); } else if(str[i] == '/') { op.push(str[i]); } } } while(!op.empty()) { char c = op.top(); op.pop(); a = num.top(); num.pop(); b = num.top(); num.pop(); if(c == '+') { num.push(a+b); } else if(c == '-') { num.push(b-a); } else if(c == '*') { num.push(a*b); } else { num.push(b/a); } } printf("%lld\n", num.top()); }
//------------------------------------------------------------------- //Method to calculate the Random Walk edge Betweenness of the input graph //------------------------------------------------------------------- void calculateEdgeBetweennessRandom() { //--- Community detection... int nCommunity = 0; for(int i=1; i<n.size(); i++) { n[i].c = 0; n[i].v = 0; n[i].w = 0; } for(int i=1; i<n.size(); i++) { for(int j=1; j<n.size(); j++) n[j].v = 0; node startNode = n[i]; node_dist.push( startNode.k ); visitedNodes(startNode.k); //--- Communities in Network bool newCommunity = false; if( n[startNode.k].c == 0) { nCommunity += 1; newCommunity = true; } while( !node_dist.empty() ) { int _key = node_dist.top(); node_dist.pop(); if(newCommunity) n[_key].c = nCommunity; } } int com_max_old = com_max; com_max = 0; for(int i=1; i<n.size(); i++) { if( n[i].c > com_max ) com_max = n[i].c; } //--- Update the matrices upDateMatrices(); vector<node> startNodes; for(int c=1; c<com_max+1; c++) { //--- Setup the sub-matrices... startNodes.clear(); //--- Get the sub-matrices getSubMatrix(c, startNodes); //--- Take a random walk within the network, //--- and calculate the edge betweenness scores... calculateRandomWalk(c, startNodes); } }
int insertR(int key, Node r_child_of_key, Node node, stack path_stack, int tid) { if (node->is_leaf) { // set the $thread_on bit to indicate thread $tid is entering. pthread_mutex_lock(&node->mutex); node->thread_on |= 0x01 << tid; pthread_mutex_unlock(&node->mutex); while (node->reorganize_bit & 0x01) { //pthread_mutex_lock(&node->mutex); //can u get this lock??? //pthread_cond_wait(&node->is_under_reorganizing, &node->mutex); //pthread_mutex_unlock(&node->mutex); } // check its own region capacity; not full is safe, and release locks // of ancestors, including parent. if (node->private_region_capacity[tid] != 0) { while (!path_stack->is_empty(path_stack)) { pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex); path_stack->pop(path_stack); } } // search in organized region (linear) int i; for (i = 0; i < node->organized_keys; ++i) { if (key == node->keys[i]) return 0; else if (key < node->keys[i]) break; } // key is between [i - 1] and [i]; follow child[i] // if it is leaf, linear search in private region (in leaf); or insert. // path_stack only contains from root to the parent of this $node. // insert the key record when $capacity != 0 if (node->private_region_capacity[tid] != 0) { int insert_position = node->private_region_index[tid] + node->private_region_keys[tid]; node->keys[insert_position] = key; //node->values[insert_position] = val; node->private_region_keys[tid] += 1; // if two threads meet the same situation and wait for each other to // complete the reorganization... $reorganize_bit $thread_on??? // how to deal with them in here. if (node->private_region_capacity[tid] == node->private_region_keys[tid]) { pthread_mutex_lock(&node->mutex); if (!(node->reorganize_bit & 0x01) && (node->private_region_capacity[tid] == node->private_region_keys[tid]) && (node->private_region_capacity[tid] != 0)) { node->reorganize_bit |= 0x01; reorganize(node); node->reorganize_bit &= 0x00; //pthread_cond_broadcast(&node->is_under_reorganizing); } pthread_mutex_unlock(&node->mutex); } } else { // split the $node //TODO: // check whether other threads are manipulating the node by // using the $thread_on in each node. And wait to lock the node. pthread_mutex_lock(&node->mutex); while (node->thread_on ^ (0x01 << tid)) { //!!!careful: if it needs to reset the $thread_on // and set it after condition wait. node->thread_on ^= 0x01 << tid; //!? right or wrong? pthread_cond_wait(&node->is_going_splitting, &node->mutex); node->thread_on |= 0x01 << tid; //!? right or wrong? } // start to split; reorganize first node->reorganize_bit |= 0x01; reorganize(node); node->reorganize_bit &= 0x00; Node u = node; Node v = createNode(1); // 1 => is leaf; Node r_subtree = r_child_of_key; int median = splitLeaf(u, v); int elem = key; int finish = 0; if (u == root) { root = createNode(0); root->keys[0] = median; root->organized_keys++; root->child[0] = u; root->child[1] = v; finish = 1; } else { elem = median; r_subtree = v; u = (Node) path_stack->top(path_stack)->data; path_stack->pop(path_stack); } pthread_mutex_unlock(&node->mutex); while (/*!path_stack->is_empty() && */!finish) { if (u->organized_keys < (order - 1)) { insertElem(elem, r_subtree, u); finish = 1; } else { v = createNode(0); median = splitNonleaf(elem, r_subtree, u, v); if (u == root) { root = createNode(0); root->keys[0] = median; root->organized_keys++; root->child[0] = u; root->child[1] = v; finish = 1; } else { pthread_mutex_unlock(&u->mutex); elem = median; r_subtree = v; u = (Node) path_stack->top(path_stack)->data; path_stack->pop(path_stack); } } } pthread_mutex_unlock(&u->mutex); } pthread_mutex_lock(&node->mutex); node->thread_on ^= 0x01 << tid; pthread_cond_signal(&node->is_going_splitting); pthread_mutex_unlock(&node->mutex); } else { // lock this node, and follow child[i] pthread_mutex_lock(&node->mutex); // if current node is safe, then release all ancestors. if (node->organized_keys < (order - 1)) { while (!path_stack->is_empty()) { pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex); path_stack->pop(path_stack); } } // search in organized region (linear) int i; for (i = 0; i < node->organized_keys; ++i) { if (key == node->keys[i]) return 0; else if (key < node->keys[i]) break; } // key is between [i - 1] and [i]; follow child[i] struct stack_node_struct stack_node; stack_node.data = node; path_stack->push(path_stack, &stack_node); insertR(key, r_child_of_key, node->child[i], path_stack); } }
void push(int x) { m_list.push(x); m_min.push(m_min.empty() ? x : min(m_min.top(), x)); }
void King::generate_moves(Board game, int x_cor, int y_cor, stack<int> &x_cans, stack<int> &y_cans) { //up-right direction int i=y_cor+1; int j=x_cor+1; if(i<8 && j<8){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } } else { x_cans.push(j); y_cans.push(i); } } //down right direction i=y_cor-1; j=x_cor+1; if (i>=0 && j<8){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } } else { x_cans.push(j); y_cans.push(i); } } //up left direction i=y_cor+1; j=x_cor-1; if (i<8 && j>=0){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } } else { x_cans.push(j); y_cans.push(i); } } //down left direction i=y_cor-1; j=x_cor-1; if (i>=0 && j>=0){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } } else { x_cans.push(j); y_cans.push(i); } } //up direction i=y_cor+1; if (i<8){ if (game.board[i][x_cor]!=0){ if (game.board[i][x_cor]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor); y_cans.push(i); } } else { x_cans.push(x_cor); y_cans.push(i); } } //down direction i=y_cor-1; if (i>=0){ if (game.board[i][x_cor]!=0){ if (game.board[i][x_cor]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor); y_cans.push(i); } } else { x_cans.push(x_cor); y_cans.push(i); } } //right direction i=x_cor+1; if (i<8){ if (game.board[y_cor][i]!=0){ if (game.board[y_cor][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor); } } else { x_cans.push(i); y_cans.push(y_cor); } } //left direction i=x_cor-1; if (i>=0){ if (game.board[y_cor][i]!=0){ if (game.board[y_cor][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor); } } else { x_cans.push(i); y_cans.push(y_cor); } } }
void Knight::generate_moves(Board game, int x_cor, int y_cor, stack<int> &x_cans, stack<int> &y_cans) { //up L's int i=y_cor+2; if (i<8){ if (x_cor-1>=0){ if (game.board[i][x_cor-1]!=0){ if (game.board[i][x_cor-1]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor-1); y_cans.push(i); } } else{ x_cans.push(x_cor-1); y_cans.push(i); } } if (x_cor+1<8){ if (game.board[i][x_cor+1]!=0){ if (game.board[i][x_cor+1]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor+1); y_cans.push(i); } } else{ x_cans.push(x_cor+1); y_cans.push(i); } } } //down L's i=y_cor-2; if (i>=0){ if (x_cor-1>=0){ if (game.board[i][x_cor-1]!=0){ if (game.board[i][x_cor-1]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor-1); y_cans.push(i); } } else{ x_cans.push(x_cor-1); y_cans.push(i); } } if (x_cor+1<8){ if (game.board[i][x_cor+1]!=0){ if (game.board[i][x_cor+1]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor+1); y_cans.push(i); } } else{ x_cans.push(x_cor+1); y_cans.push(i); } } } //left L's i=x_cor-2; if (i>=0){ if (y_cor-1>=0){ if (game.board[y_cor-1][i]!=0){ if (game.board[y_cor-1][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor-1); } } else{ x_cans.push(i); y_cans.push(y_cor-1); } } if (y_cor+1<8){ if (game.board[y_cor+1][i]!=0){ if (game.board[y_cor+1][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor+1); } } else{ x_cans.push(i); y_cans.push(y_cor+1); } } } //right L's i=x_cor+2; if (i<8){ if (y_cor-1>=0){ if (game.board[y_cor-1][i]!=0){ if (game.board[y_cor-1][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor-1); } } else{ x_cans.push(i); y_cans.push(y_cor-1); } } if (y_cor+1<8){ if (game.board[y_cor+1][i]!=0){ if (game.board[y_cor+1][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor+1); } } else{ x_cans.push(i); y_cans.push(y_cor+1); } } } }
NativeBlockPtr decodeBlock( ExecutableContainer *c, ExternalFunctionMap &f, LLVMByteDecoder &d, stack<VA> &blockChildren, VA e, stack<VA> &funcs, raw_ostream &out) { NativeBlockPtr B = NativeBlockPtr(new NativeBlock(e, d.getPrinter())); VA curAddr = e; bool has_follow = true; out << "Processing block: " << B->get_name() << "\n"; do { InstPtr I = d.getInstFromBuff(curAddr, c); //I, if a terminator, will have true and false targets //filled in. I could be an indirect branch of some kind, //we will deal with that here. we will also deal with the //instruction if it is a data instruction with relocation out << to_string<VA>(I->get_loc(), hex) << ":"; out << I->printInst() << "\n"; if(I->get_tr() != 0) { B->add_follow(I->get_tr()); has_follow = false; out << "Adding block: " << to_string<VA>(I->get_tr(), hex) << "\n"; blockChildren.push(I->get_tr()); } if(I->get_fa() != 0) { B->add_follow(I->get_fa()); has_follow = false; out << "Adding block: " << to_string<VA>(I->get_fa(), hex) << "\n"; blockChildren.push(I->get_fa()); } if(I->terminator()) { has_follow = false; } //do we need to add a data reference to this instruction? //again, because there is no offset information in the //instruction decoder, for now we just ask if every addr //in the inst is relocated for(uint32_t i = 0; i < I->get_len(); i++) { VA addrInInst = curAddr+i; if(c->is_addr_relocated(addrInInst)) { VA addr = 0; std::string has_imp; // this instruction has a relocation // save the relocation offset for later I->set_reloc_offset(i); //get the offset for this address //add it as a data offset to the instruction if (c->find_import_name(addrInInst, has_imp) ) { if(f.is_data(has_imp)) { ExternalDataRefPtr data_p = makeExtDataRefFromString(has_imp, f); out << "Adding external data ref: " << has_imp << "\n"; I->set_ext_data_ref(data_p); } else { ExternalCodeRefPtr code_p = makeExtCodeRefFromString(has_imp, f); LASSERT(code_p, "Failed to get ext call from map for symbol: "+has_imp); //maybe, this call doesn't return, in which case, //we should kill the decoding of this flow if(code_p->getReturnType() == ExternalCodeRef::NoReturn) { has_follow = false; } out << "Adding external code ref: " << has_imp << "\n"; I->set_ext_call_target(code_p); } } else if(c->relocate_addr(addrInInst, addr)) { bool can_ref_code = canInstructionReferenceCode(I); bool is_reloc_code = isAddrOfType(c, addr, ExecutableContainer::CodeSection); bool is_reloc_data = isAddrOfType(c, addr, ExecutableContainer::DataSection); unsigned opc = I->get_inst().getOpcode(); if(isBranchViaMemory(I)) { out << "Detect branch via memory, relocation handled later\n"; } // this instruction can reference code and does // reference code // so we assume the code points to a function else if( can_ref_code && is_reloc_code ) { list<VA> new_funcs; if(dataInCodeHeuristic(c, I, addr, new_funcs)) { // add new functions to our functions list for(list<VA>::const_iterator nfi = new_funcs.begin(); nfi != new_funcs.end(); nfi++) { funcs.push(*nfi); } I->set_data_offset(addr); } else { I->set_call_tgt(addr); out << "Adding: 0x" << to_string<VA>(addr, hex) << " as target\n"; funcs.push(addr); } } // this instruction can't reference code and points to .text // or references data. Treat as data element // TODO: extract this from .text and shove into .data? else if(( !can_ref_code && is_reloc_code) || is_reloc_data ) { I->set_data_offset(addr); } else { out << "WARNING: relocation points to neither code nor data:" << to_string<VA>(addr, hex) << "\n"; } } else { out << "*NOT* Relocating relocatable addr:" << to_string<uint32_t>(addrInInst, hex) << "\n"; } break; } } //is this instruction an external call? //in a COFF binary, the pcrel call can refer to an //external symbol that has been relocated //so, get the string that corresponds, and //provide the translation using the function map MCOperand op; string imp; switch(I->get_inst().getOpcode()) { case X86::JMP32m: { string thunkSym; bool r = c->find_import_name(curAddr+2, thunkSym); if(r) { // this goes to an external API call out << "Adding external code ref via JMP: " << thunkSym << "\n"; ExternalCodeRefPtr p = makeExtCodeRefFromString(thunkSym, f); I->set_ext_call_target(p); has_follow = false; } else { // this is an internal jmp. probably a jump table. bool did_jmptable = handlePossibleJumpTable(c, B, I, curAddr, funcs, blockChildren, out); LASSERT(did_jmptable, "JMP32m processing aborted: couldn't parse jumptable"); } } break; case X86::CALLpcrel32: //this could be an external call in COFF, or not op = I->get_inst().getOperand(0); LASSERT(op.isImm(), "Nonsense for CALLpcrel32"); if(op.getImm() !=0) { VA callTgt = curAddr+op.getImm()+I->get_len(); bool foldFunc = false; //speculate about callTgt InstPtr spec = d.getInstFromBuff(callTgt, c); if(spec->terminator() && spec->get_inst().getOpcode() == X86::JMP32m) { string thunkSym; bool r = c->find_import_name(callTgt+2, thunkSym); LASSERT(r, "Need to find thunk import addr"); ExternalCodeRefPtr p = makeExtCodeRefFromString(thunkSym, f); I->set_ext_call_target(p); foldFunc = true; if(p->getReturnType() == ExternalCodeRef::NoReturn) { has_follow = false; } } if(foldFunc == false) { //add this to our list of funcs to search funcs.push(callTgt); } } else { //check to see if this is an external call... if(I->has_ext_call_target() == false) { // may be a local call VA addr=curAddr+1, relo_addr=0; out << "Symbol not found, maybe a local call\n"; if(c->relocate_addr(addr, relo_addr)){ out << "Found local call to: " << to_string<VA>(relo_addr, hex) << "\n"; I->set_call_tgt(relo_addr); out << "Adding: 0x" << to_string<VA>(relo_addr, hex) << " as target\n"; funcs.push(relo_addr); } else { out << "Could not relocate addr for local call at: "; out << to_string<VA>(curAddr, hex) << "\n"; } } else { out << "External call to: " << I->get_ext_call_target()->getSymbolName() << "\n"; } } break; case X86::CALL32m: //this should be a call to an external, or we have no idea //so we need to try and look up the symbol that we're calling at this address... if(c->find_import_name(curAddr+2, imp)) { ExternalCodeRefPtr p = makeExtCodeRefFromString(imp, f); LASSERT(p, "Failed to get ext call from map for symbol"+imp); out << "Calling symbol: " << p->getSymbolName() << "\n"; if(p->getReturnType() == ExternalCodeRef::NoReturn) { has_follow = false; } I->set_ext_call_target(p); } else { out << "Cannot find symbol at address "; out << to_string<VA>(curAddr, hex) << "\n"; } break; } B->add_inst(I); curAddr += I->get_len(); } while(has_follow); //we have built a basic block, it might contain //multiple calls, but it only has one terminator //which is either a ret or a branch return B; }
void startEntryEvent(int id) { dprintf("---------> starting Entry Event with id: %d\n", id); if ((id == -1) || (events[id] == NULL)) { dprintf("-------> create event with id: %d\n", id); //sprintf(name, "Event %d", id); if (id == -1) { /* char *name = "dummy_thread_ep"; dprintf(" ------> creating event: %s\n", name); TAU_PROFILER_CREATE(events[id], name, "", TAU_DEFAULT); dprintf("timer created.\n"); eventStack.push(events[id]); dprintf(" ------> starting event: %s\n", (char*) name); TAU_PROFILER_START(eventStack.top());*/ //exclude dummy event dprintf("------> excluding dummy function"); eventStack.push(EXCLUDED); } else { //string check("doFFT(RSFFTMsg* impl_msg)"); //string name_s(_entryTable[id]->name); //printf("checking name4: %s", _entryTable[id]->name); //if (check.compare(name_s) != 0) //{ char name [500]; sprintf(name, "%s::%s::%d", _chareTable[_entryTable[id]->chareIdx]->name, _entryTable[id]->name, id); //should this fuction be excluded from instrumentation? if (!instrumentEntity(name)) { //exclude function. dprintf("------> excluding function %s\n", name); events[id] = EXCLUDED; eventStack.push(events[id]); } else { dprintf(" ------> creating event: %s\n", name); TAU_PROFILER_CREATE(events[id], name, "", TAU_DEFAULT); dprintf("timer created.\n"); eventStack.push(events[id]); dprintf("starting event\n"); dprintf(" ------> starting event: %s\n", (char*) name); TAU_PROFILER_START(eventStack.top()); } dprintf("done.\n"); } } else { eventStack.push(events[id]); if (events[id] != EXCLUDED) { TAU_PROFILER_START(eventStack.top()); } } }
void join(double val) { in.push(val); }
inline void Push(int x) { mark[x]=1; ins[x]=1; s.push(x); }
int main() { freopen("5575.in", "r", stdin); scanf("%d", &T); for (int cas = 1; cas <= T; cas++) { scanf("%d %d", &n, &m); for (int i = 1; i < n; i++) scanf("%d", &h[i]); for (int i = 0; i <= n; i++) { sum[0][i] = sum[1][i] = dp[i] = 0; while (!q[i].empty()) q[i].pop(); } while (!st.empty()) st.pop(); st.push(0); h[0] = h[n] = INF; int a, b, c; for (int i = 0; i < m; i++) { scanf("%d %d %d", &a, &b, &c); q[a].push(Node(b + 1, c)); sum[0][a] += !c; } for (int i = 1; i <= n; i++) { while (!st.empty() && h[i] >= h[st.top()]) { int top = st.top(); st.pop(); int cnt0 = 0, cnt1 = 0; int ans = 0; int last = 0; while (!q[i].empty() && q[i].top().y <= h[top]) { Node t = q[i].top(); q[i].pop(); if (t.y != last) { sum[0][i] -= cnt0; sum[1][i] += cnt1; cnt0 = cnt1 = 0; ans = max(ans, sum[0][i] + sum[1][i]); dp[i] = max(dp[i], dp[top] + ans); } cnt0 += !t.t; cnt1 += t.t; last = t.y; } sum[0][i] -= cnt0; sum[1][i] += cnt1; ans = max(ans, sum[0][i] + sum[1][i]); dp[i] = max(dp[i], dp[top] + ans); sum[0][i] += sum[0][top]; sum[1][i] += sum[1][top]; q[i].join(q[top]); } if (!st.empty()) { int top = st.top(); int cnt0 = 0, cnt1 = 0; int ans = 0; int last = 0; while (!q[i].empty() && q[i].top().y <= h[i]) { Node t = q[i].top(); q[i].pop(); if (t.y != last) { sum[0][i] -= cnt0; sum[1][i] += cnt1; cnt0 = cnt1 = 0; ans = max(ans, sum[0][i] + sum[1][i]); dp[i] = max(dp[i], dp[top] + ans); } cnt0 += !t.t; cnt1 += t.t; last = t.y; } sum[0][i] -= cnt0; sum[1][i] += cnt1; ans = max(ans, sum[0][i] + sum[1][i]); dp[i] = max(dp[i], dp[top] + ans); } st.push(i); } printf("Case #%d: %d\n", cas, dp[n]); } return 0; }
// Push element x to the back of queue. void push(int x) { S1.push(x); }
void Work(string str) { int i,Error = 0; string tmp = ""; for(i = str.size()-1; i >= 0; i--) { if(str[i] == ' ') { if(tmp == "+") opStack.push('+'); else if(tmp == "-") opStack.push('-'); else if(tmp == "*") opStack.push('*'); else if(tmp == "/") opStack.push('/'); else valStack.push(Convert(tmp)); tmp = ""; while(valStack.size() >= 2 && opStack.size() >= 1) {//进行运算 double a = valStack.top(); valStack.pop(); double b = valStack.top(); valStack.pop(); char op = opStack.top(); opStack.pop(); if(op == '+') valStack.push(a+b); else if(op == '-') valStack.push(a-b); else if(op == '*') valStack.push(a*b); else if(op == '/') { if(b==0) { Error = 1; break; } else valStack.push(a/b); } } } else { tmp += str[i]; } } if(Error == 1) { puts("ERROR"); return; } if(tmp == "+") opStack.push('+'); else if(tmp == "-") opStack.push('-'); else if(tmp == "*") opStack.push('*'); else if(tmp == "/") opStack.push('/'); else valStack.push(Convert(tmp)); tmp = ""; while(valStack.size() >= 2 && opStack.size() >= 1) {//进行运算 double a = valStack.top(); valStack.pop(); double b = valStack.top(); valStack.pop(); char op = opStack.top(); opStack.pop(); if(op == '+') valStack.push(a+b); else if(op == '-') valStack.push(a-b); else if(op == '*') valStack.push(a*b); else if(op == '/') { if(b==0) { Error = 1; break; } else valStack.push(a/b); } } if(Error == 1) { puts("ERROR"); return; } if(opStack.size() == 0 && valStack.size() == 1) { printf("%.1lf\n", valStack.top()); } else { puts("ERROR"); } }
void Rook::generate_moves(Board game, int x_cor, int y_cor, stack<int> &x_cans, stack<int> &y_cans) { //up direction int i=y_cor+1; while(i<8){ if (game.board[i][x_cor]!=0){ if (game.board[i][x_cor]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor); y_cans.push(i); } break; } else { x_cans.push(x_cor); y_cans.push(i); } i++; } //down direction i=y_cor-1; while(i>=0){ if (game.board[i][x_cor]!=0){ if (game.board[i][x_cor]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(x_cor); y_cans.push(i); } break; } else { x_cans.push(x_cor); y_cans.push(i); } i--; } //right direction i=x_cor+1; while(i<8){ if (game.board[y_cor][i]!=0){ if (game.board[y_cor][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor); } break; } else { x_cans.push(i); y_cans.push(y_cor); } i++; } //left direction i=x_cor-1; while(i>=0){ if (game.board[y_cor][i]!=0){ if (game.board[y_cor][i]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(i); y_cans.push(y_cor); } break; } else { x_cans.push(i); y_cans.push(y_cor); } i--; } }
void XapianEngine::stackQuery(const QueryProperties &queryProps, stack<Xapian::Query> &queryStack, const string &stemLanguage, bool followOperators) { Xapian::Query::op queryOp = Xapian::Query::OP_OR; string term; // Get the terms to AND together if (queryProps.getAndWords().empty() == false) { vector<string> andTerms; if (extractWords(queryProps.getAndWords(), stemLanguage, andTerms) == true) { #ifdef DEBUG cout << "XapianEngine::stackQuery: OP_AND " << andTerms.size() << endl; #endif if (followOperators == true) { queryOp = Xapian::Query::OP_AND; } queryStack.push(Xapian::Query(queryOp, andTerms.begin(), andTerms.end())); } } // Get the terms of the phrase if (queryProps.getPhrase().empty() == false) { vector<string> phraseTerms; if (extractWords(queryProps.getPhrase(), stemLanguage, phraseTerms) == true) { #ifdef DEBUG cout << "XapianEngine::stackQuery: OP_PHRASE " << phraseTerms.size() << endl; #endif if (followOperators == true) { queryOp = Xapian::Query::OP_PHRASE; } queryStack.push(Xapian::Query(queryOp, phraseTerms.begin(), phraseTerms.end())); } } // Get the terms to OR together if (queryProps.getAnyWords().empty() == false) { vector<string> orTerms; if (extractWords(queryProps.getAnyWords(), stemLanguage, orTerms) == true) { #ifdef DEBUG cout << "XapianEngine::stackQuery: OP_OR " << orTerms.size() << endl; #endif if (followOperators == true) { queryOp = Xapian::Query::OP_OR; } queryStack.push(Xapian::Query(queryOp, orTerms.begin(), orTerms.end())); } } // Get the terms to NOT together if (queryProps.getNotWords().empty() == false) { vector<string> notTerms; if (extractWords(queryProps.getNotWords(), stemLanguage, notTerms) == true) { #ifdef DEBUG cout << "XapianEngine::stackQuery: OP_AND_NOT " << notTerms.size() << endl; #endif // We need something to AND_NOT these terms against // Not following the operator would make us return documents // that have terms the user isn't interested in Xapian::Query notQuery(Xapian::Query::OP_AND, notTerms.begin(), notTerms.end()); if (queryStack.empty() == false) { Xapian::Query topQuery = queryStack.top(); queryStack.pop(); queryStack.push(Xapian::Query(Xapian::Query::OP_AND_NOT, topQuery, notQuery)); } } } // Get the host name filter if (queryProps.getHostFilter().empty() == false) { vector<string> hostTerms; term = "H"; term += StringManip::toLowerCase(queryProps.getHostFilter()); hostTerms.push_back(term); if (followOperators == true) { queryOp = Xapian::Query::OP_AND; } queryStack.push(Xapian::Query(queryOp, hostTerms.begin(), hostTerms.end())); } // Get the file name filter if (queryProps.getFileFilter().empty() == false) { vector<string> fileTerms; term = "P"; term += StringManip::toLowerCase(queryProps.getFileFilter()); fileTerms.push_back(term); if (followOperators == true) { queryOp = Xapian::Query::OP_AND; } queryStack.push(Xapian::Query(queryOp, fileTerms.begin(), fileTerms.end())); } // Get the label name filter if (queryProps.getLabelFilter().empty() == false) { vector<string> labelTerms; term = "XLABEL:"; term += queryProps.getLabelFilter(); labelTerms.push_back(term); if (followOperators == true) { queryOp = Xapian::Query::OP_AND; } queryStack.push(Xapian::Query(queryOp, labelTerms.begin(), labelTerms.end())); } // Get the language filter string language = queryProps.getLanguage(); if (language.empty() == false) { vector<string> languageTerms; term = "L"; term += Languages::toCode(Languages::toEnglish(language)); #ifdef DEBUG cout << "XapianEngine::stackQuery: filter " << term << endl; #endif languageTerms.push_back(term); if (followOperators == true) { queryOp = Xapian::Query::OP_AND; } queryStack.push(Xapian::Query(queryOp, languageTerms.begin(), languageTerms.end())); } }
void Bishop::generate_moves(Board game, int x_cor, int y_cor, stack<int> &x_cans, stack<int> &y_cans) { //up-right direction int i=y_cor+1; int j=x_cor+1; while(i<8 && j<8){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } break; } else { x_cans.push(j); y_cans.push(i); } i++; j++; } //down right direction i=y_cor-1; j=x_cor+1; while(i>=0 && j<8){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } break; } else { x_cans.push(j); y_cans.push(i); } i--; j++; } //up left direction i=y_cor+1; j=x_cor-1; while(i<8 && j>=0){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } break; } else { x_cans.push(j); y_cans.push(i); } i++; j--; } //down left direction i=y_cor-1; j=x_cor-1; while(i>=0 && j>=0){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack!=game.board[y_cor][x_cor]->isBlack){ x_cans.push(j); y_cans.push(i); } break; } else { x_cans.push(j); y_cans.push(i); } i--; j--; } }
void push(int a){ lock_guard<mutex> lock(mx); st.push(a); }
void Pawn::generate_moves(Board game, int x_cor, int y_cor, stack<int> &x_cans, stack<int> &y_cans) { //Black moves down //board is upside-down, so down is moving up the array if (game.board[y_cor][x_cor]->isBlack==true){ int i=y_cor+1; if (i<8){ if (game.board[i][x_cor]==0){ x_cans.push(x_cor); y_cans.push(i); } int j=x_cor+1; if (j<8){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack==false){ x_cans.push(j); y_cans.push(i); } } } j=x_cor-1; if (j>=0){ if (game.board[i][j]!=0){ if (game.board[i][j]->isBlack==false){ x_cans.push(j); y_cans.push(i); } } } } //Can move down two at start if (y_cor==1){ if (game.board[i][x_cor]==0){ if (game.board[i+1][x_cor]==0){ x_cans.push(x_cor); y_cans.push(i+1); } } } } //White moves up //board is upside-down, so moving up is moving down the array if (game.board[y_cor][x_cor]->isBlack==false){ int i=y_cor-1; if (i>=0){ if (game.board[i][x_cor]==0){ x_cans.push(x_cor); y_cans.push(i); } if (x_cor+1<8){ if (game.board[i][x_cor+1]!=0){ if (game.board[i][x_cor+1]->isBlack==true){ x_cans.push(x_cor+1); y_cans.push(i); } } } if (x_cor-1>=0){ if (game.board[i][x_cor-1]!=0){ if (game.board[i][x_cor-1]->isBlack==true){ x_cans.push(x_cor-1); y_cans.push(i); } } } } //Can move up two at start if (y_cor==6){ if (game.board[y_cor-1][x_cor]==0){ if (game.board[y_cor-2][x_cor]==0){ x_cans.push(x_cor); y_cans.push(y_cor-2); } } } } }
void dfs (int u) { if (u == ((1<<n)-1)) return; stk.push(shoot[u]); dfs(father[u]); }
void push(stack<int> &newstack,int data) { newstack.push(data); }
// enqueue an element to the queue void queue :: enqueue(int element) { s1.push(element); }
void printToken( FILE * listing, TokenType token, TokenStruct & tokenStruct ) { switch (token) { // Tags case OPENTAG: switchflag = true; checktoken.push(token); break; case CLOSETAG: switchflag = false; if(!checktoken.empty()&&checktoken.top()==OPENTAG) checktoken.pop(); else fprintf(listing, "TAGERROR: TAG, Row: %d\n", tokenStruct.row); break; case OPENDOCNO: usefulinfo = true; if(!switchflag) { fprintf( listing, "$ID\n" ); numkeep = true; } checktoken.push(token); break; case CLOSEDOCNO: usefulinfo = false; numkeep = false; if(!checktoken.empty()&&checktoken.top()==OPENDOCNO) checktoken.pop(); else fprintf(listing, "TAGERROR: DOCNO, Row: %d\n", tokenStruct.row); break; case OPENTEXT: usefulinfo = true; if(!switchflag) fprintf( listing, "$NARR\n" ); checktoken.push(token); break; case CLOSETEXT: usefulinfo = false; if(!checktoken.empty()&&checktoken.top()==OPENTEXT) checktoken.pop(); else fprintf(listing, "TAGERROR: TEXT, Row: %d\n", tokenStruct.row); break; case OPENDOC: usefulinfo = true; if(!switchflag) fprintf( listing, "$DESC\n" ); checktoken.push(token); break; case CLOSEDOC: usefulinfo = false; if(!checktoken.empty()&&checktoken.top()==OPENDOC) checktoken.pop(); else fprintf(listing, "TAGERROR: DOC, Row: %d\n", tokenStruct.row); break; case OPENGRAPHIC: usefulinfo = true; if(!switchflag) fprintf( listing, "$DESC\n" ); checktoken.push(token); break; case CLOSEGRAPHIC: usefulinfo = false; if(!checktoken.empty()&&checktoken.top()==OPENGRAPHIC) checktoken.pop(); else fprintf(listing, "TAGERROR: DOC, Row: %d\n", tokenStruct.row); break; case OPENHEADLINE: usefulinfo = true; if(!switchflag) fprintf( listing, "$TITLE\n" ); checktoken.push(token); break; case CLOSEHEADLINE: usefulinfo = false; if(!checktoken.empty()&&checktoken.top()==OPENHEADLINE) checktoken.pop(); else fprintf(listing, "TAGERROR: HEADLINE, Row: %d\n", tokenStruct.row); break; case OPENOTHERTAG: checktoken.push(token); break; case CLOSEOTHERTAG: if(!checktoken.empty()&&checktoken.top()==OPENOTHERTAG) checktoken.pop(); else fprintf(listing, "TAGERROR: OTHER, Row: %d\n", tokenStruct.row); break; //Punctuation case PERIOD: break; case QUESTION: break; case EXCLAMATION: break; case OTHERMARK: break; case ENDFILE: fprintf( listing, "EOF\n" ); break; case NUMBER: if(!switchflag&&numkeep&&usefulinfo) fprintf( listing, "%s\n", tokenStruct.value.c_str() ); break; case WORD: { if(!switchflag&&usefulinfo) { string s = lowerstr(tokenStruct.value); if(!checkStopwords(s)) fprintf( listing, "%s\n", s.c_str()); } } break; case APOSTROPHIZED: { if(!switchflag&&usefulinfo) { string s = lowerstr(tokenStruct.value); if(!checkStopwords(s)&&s.length()>3) fprintf( listing, "%s\n", s.c_str()); } } break; case TYPO: { if(!switchflag&&usefulinfo) { string s = lowerstr(tokenStruct.value); string f,b; int pos = s.find("'"); f = s.substr(0,pos); b = s.substr(pos+1,s.length()); if(!checkStopwords(f)) fprintf( listing, "%s\n", f.c_str()); if(!checkStopwords(b)) fprintf( listing, "%s\n", b.c_str()); } } break; case HYPHENATED: { if(!switchflag&usefulinfo) { string s = lowerstr(tokenStruct.value); string f,b; int pos = s.find("-"); f = s.substr(0,pos); b = s.substr(pos+1,s.length()); if(f.length()>3&&b.length()>3) { if(!checkStopwords(f)) fprintf(listing, "%s\n", f.c_str()); if(!checkStopwords(b)) fprintf(listing, "%s\n", b.c_str()); } else fprintf( listing, "%s\n", s.c_str()); } } break; case ERROR: if(!switchflag&usefulinfo) fprintf( listing, "ERROR: %s\n", tokenStruct.value.c_str() ); break; default: // should never happen fprintf( listing, "Unknown token: %d\n", token ); } }
void add(int d) { if (!disks.empty() && disks.top()<=d) cout<<"Error placing disk "<<d<<endl; else disks.push(d); }
void push(int element) { stack1.push(element); }
void push(int elem) { s1.push(elem); }
void enqueue(int val) { in.push(val); }
void Push(int v) { st1.push(v); }
//------------------------------------------------------------------- // Method to calculate the Geodesic edge Betweenness of the input graph //------------------------------------------------------------------- void calculateEdgeBetweennessGeodesic() { int nCommunity = 0; queue<int> tops ; temp_score [n.size()]; //--- Reset node properties after removing the edge. for(int i=1; i < n.size(); i++) { n[i].c = 0; tops.push(n[i].k); } //--- Loop over every node in turn, treating as the //--- root node. while ( !tops.empty() ) { for(int i=0; i<elist.size(); i++) elist[i].we = 0; int top = tops.front(); tops.pop(); node_dist.push( top ); for(int i=1; i<n.size(); i++) { n[i].w = 0.0; n[i].d = 0.0; temp_score[i] = 0.0; } n[top].w = 1.0; n[top].d = 0.0; //-- Assign Node weights & distances assignNodeWeights(top); //-- Communities in Network bool newCommunity = false; if( n[top].c == 0 ) { nCommunity += 1; newCommunity = true; } //-- Assign Edges Weights while( !node_dist.empty() ) { int _key = node_dist.top(); node_dist.pop(); if(newCommunity) n[_key].c = nCommunity; if( n[_key].d < 1 ) continue; vector<edge> edges = n[_key].getEdges(); for( int i=0; i<edges.size(); i++ ) { int ind_j = edges[i].si; if( edges[i].si == _key ) ind_j = edges[i].so; if( (n[ind_j].d == n[_key].d-1) && n[ind_j].w > 0 ) { temp_score[ind_j] += (temp_score[_key]+1) * (n[ind_j].w / n[_key].w); elist[edges[i].key-1].we += elist[edges[i].key-1].Globalwe * ( (temp_score[_key]+1) * (n[ind_j].w / n[_key].w) ); totallist[edges[i].key].we += elist[edges[i].key-1].we; } } } } }
void concat(stack<int> & l1,list<int> l2){ for (list<int>::iterator iter = l2.begin();iter != l2.end();iter++){ l1.push(*iter); } }