华企在线网站建设,app 软件开发,百度seo优化排名如何,昆明专业的网站制作建设题目链接如下#xff1a;
Online Judge
这道题我一开始的思路大方向其实是对的#xff0c;但细节怎么实现set到int的哈希没能想清楚#xff08;没想到这都能用map#xff09;。用setstring的做法来做#xff0c;测试数据小的话答案是对的#xff0c;但大数据时…题目链接如下
Online Judge
这道题我一开始的思路大方向其实是对的但细节怎么实现set到int的哈希没能想清楚没想到这都能用map。用setstring的做法来做测试数据小的话答案是对的但大数据时间超时。
其实就是把所有set一一映射到int, 所以stack里每个元素就是int.
按照刘汝佳思路写的代码如下按理每个case里stack应该先清空但因为题目保证了没有无效操作只需要最上面set的元素个数不清空也没问题
#include cstdio
#include set
#include stack
#include map
#include vector
// #define debugint T, N, a, b;
std::mapstd::setint, int mp;
char op[10];
std::stackint s;
std::setint empty;
std::vectorstd::setint vec;void _push(std::setint st){if(!mp.count(st)){vec.push_back(st);mp[st] vec.size() - 1;}s.push(mp[st]);
}int main(){#ifdef debugfreopen(0.txt, r, stdin);freopen(1.txt, w, stdout);#endifscanf(%d, T);while(T--){scanf(%d, N);vec.clear();mp.clear();while(N--){scanf(%s, op);if(op[0] P){_push(empty);} else{a s.top();s.pop();if(op[0] D){s.push(a);s.push(a);} else{b s.top();s.pop();std::setint tmp;if(op[0] A){tmp vec[b];tmp.insert(a);} else{if(op[0] U){tmp vec[a];for(auto it vec[b].begin(); it ! vec[b].end(); it){tmp.insert(*it);}} else if(op[0] I){for(auto it vec[a].begin(); it ! vec[a].end(); it){if(vec[b].find(*it) ! vec[b].end()){tmp.insert(*it);}}}}_push(tmp);}}printf(%d\n, vec[s.top()].size());}printf(***\n);}#ifdef debugfclose(stdin);fclose(stdout);#endifreturn 0;
}
原先的代码如下超时
#include cstdio
#include set
#include stack
#include string
// #define debugint T, N;
char op[10];
std::stackstd::setstd::string s, empStack;
std::setstd::string a, b, empty;std::string toString(std::setstd::string st){std::string str {;for(auto it st.begin(); it ! st.end(); it){str (it st.begin() ? : ,);str *it;}return str };
}int main(){#ifdef debugfreopen(0.txt, r, stdin);freopen(1.txt, w, stdout);#endifscanf(%d, T);while(T--){scanf(%d, N);s.swap(empStack);while(N--){scanf(%s, op);if(op[0] P){s.push(empty);} else{a s.top();s.pop();if(op[0] D){s.push(a);s.push(a);} else{b s.top();s.pop();if(op[0] A){b.insert(toString(a));s.push(b);} else{if(op[0] U){for(auto it a.begin(); it ! a.end(); it){b.insert(*it);}s.push(b);} else if(op[0] I){std::setstd::string intersect;for(auto it a.begin(); it ! a.end(); it){if(b.find(*it) ! b.end()){intersect.insert(*it);}}s.push(intersect);}}}}printf(%d\n, s.top().size());}printf(***\n);}#ifdef debugfclose(stdin);fclose(stdout);#endifreturn 0;
}
文章转载自: http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.yrjxr.cn.gov.cn.yrjxr.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.tsmcc.cn.gov.cn.tsmcc.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.mjglk.cn.gov.cn.mjglk.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn