SGU 271 好久不写题,脑袋生锈了 奇怪的队列。。
来源:互联网 发布:手机淘宝 装修日记 编辑:程序博客网 时间:2024/04/28 00:31
可以逆置前 K 个书名。
操作比较多,一看就是数据结构的题目。
操作只涉及前 K 个,所以,我用一个栈来保存不会被操作的数据。需要被逆置或者增加的数据我使用一个可以交换队头队尾的队列来完成。
队列的大小是 K ,如果满了就从队头(最下面的那本书)里拿出一个放入栈中。
逆序就是交换头尾而已。
这样就 OK 了。
#include <stdio.h>#include <iostream>#include <queue>#include <algorithm>#include <map>#include <vector>#include <cmath>#include <string.h>#include <stdlib.h>#include <time.h>#include <fstream>#include <set>#include <stack>#include <deque>#include <list>using namespace std;#define READ freopen("acm.in","r",stdin)#define WRITE freopen("acm.out","w",stdout)#define ll long long#define ull unsigned long long #define uint unsigned int#define PII pair<int,int>#define PDD pair<double,double>#define fst first#define sec second#define MS(x,d) memset(x,d,sizeof(x))#define INF 0x3f3f3f3f#define ALL(x) x.begin(),x.end()#define PB push_back#define MOD 99991#define MAX 1000000stack<string> S;int n,m,k;struct tmpQ{ string q[200000]; int f,b; int state; tmpQ() { state=0; f=100000; b=f+1; } void rotate() { state=!state; } void push(string s) { if(state) q[f--]=s; else q[b++]=s; if(size()>k) { S.push(front()); pop_front(); } } bool empty() { return size()<=0; } string front() { if(state) return q[b-1]; else return q[f+1]; } void pop_front() { if(state) b--; else f++; } int size() { return b-f-1; }}Q;int main(){ //freopen("acm.in","r",stdin); scanf("%d%d%d",&n,&m,&k); stack<string> tmpS; for(int i=0;i<n;i++) { char c[123]; scanf("%s",c); string ss(c); tmpS.push(ss); } while(!tmpS.empty()) Q.push(tmpS.top()),tmpS.pop(); for(int i=0;i<m;i++) { char c[1231]; scanf("%s",c); if(c[0]=='A') { string s; bool flag=false; for(int i=0;c[i]!=')';i++) { if(flag) s+=c[i]; if(c[i]=='(') flag=true; } Q.push(s); } else { Q.rotate(); } } while(!Q.empty()) { S.push(Q.front()); Q.pop_front(); } while(!S.empty()) cout<<S.top()<<endl,S.pop(); return 0;}
0 0
- SGU 271 好久不写题,脑袋生锈了 奇怪的队列。。
- 脑袋要生锈了
- 那个脑袋生锈的我写的东西
- 脑子生锈了
- 趣味推理题 动动好久没有认真思考的脑袋
- 【JZOJ5456】奇怪的队列
- 等了好久才解决的题,还是看了别人代码的,优先队列
- 今天感觉脑袋不是我的了
- 传了好久的照片
- 纠结了好久的递归
- 做做--爱因斯坦出的题,活动活动脑子,不然生锈了啊~
- 【poj2008】奇怪的优先队列
- CSDN的帐号注册的好久好久了
- 脑袋都大了
- 脑袋堵住了
- 数据结构 【joseph环】 写了好久好久的~
- SGU 271 Book Pile 双端队列
- SGU 271 双端队列deque
- 泛型
- linux搭建hadoop环境
- 数据结构-单链表 读书笔记
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- 黑马程序员13——银行管理系统
- SGU 271 好久不写题,脑袋生锈了 奇怪的队列。。
- 2-36进制的 两个数相加
- 【leetcode】3SumClosest
- uva11205
- 二叉树的遍历问题
- ArcGIS10中根据shapefile裁剪栅格影像
- 10个调试和排错的小建议
- IO流
- UVA 11996 Jewel Magic splay+字符串hash