Basic Data Structure
来源:互联网 发布:net 查看端口 编辑:程序博客网 时间:2024/05/19 23:12
题意:给一个栈,有push , pop , reverse , query 。 query求从栈顶到栈底逐个求 NAND 的值, 如果栈为空就输出 Invalid.
解法:可以用双向队列来模拟操作,每次都记录最接近栈底的0的位置(两边都要),那么对于QUERY操作,如果存在0位置,如果在0之前有数字,那么到0这个位置就会变成1, 不然就是0。然后从0的位置往后一定都是1,对于每一个1,答案都会取反一次,所以就相当于数一下1的个数奇偶情况就可以了。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <iostream>using namespace std;const int maxn = 600000;int a[maxn], len, head, tail, n, t, cnt, k;int b[maxn], hh, tt;char s[10];int main() { int T; scanf("%d", &T); for (int cases = 1; cases <= T; cases++) { printf("Case #%d:\n", cases); scanf("%d", &n); memset(a, 0, sizeof(a)); head = tail = 300000; hh = tt = 300000; k = 1; while (n--) { scanf("%s", s); len = strlen(s); if (len == 4) { scanf("%d", &t); if (k == 1) { a[tail] = t; tail++; if (t == 0) { b[tt] = tail-1; tt++; } } else { head--; a[head] = t; if (t == 0) { hh--; b[hh] = head; } } } else if (len == 3) { if (k == 1) { tail--; if (a[tail] == 0) tt--; } else { if (a[head] == 0) hh++; head++; } } else if (len == 7) { k = -k; } else if (len == 5) { if (head == tail) { printf("Invalid.\n"); } else { if (k == 1) { if (hh == tt) { len = tail-head; if (len%2 == 1) printf("1\n"); else printf("0\n"); } else { len = b[hh]-head+1; if (b[hh] == tail-1) len--; if (len%2 == 1) printf("1\n"); else printf("0\n"); } } else { if (hh == tt) { len = tail-head; if (len%2 == 1) printf("1\n"); else printf("0\n"); } else { len = tail-b[tt-1]; if (b[tt-1] == head) len--; if (len%2 == 1) printf("1\n"); else printf("0\n"); } } } } } }}
0 0
- Basic Data Structure summary
- Basic Data Structure
- Basic Data Structure
- Basic Data Structure
- HDU5929-Basic Data Structure
- Basic Tree--Data Structure
- Basic Data Structure
- hdu 5929 Basic Data Structure
- Basic Data Structure hdu 5929
- HDU 5292 Basic Data Structure
- HDU-5929 Basic Data Structure
- Algorithms&Data Structure:Basic Sort
- HDU 5929Basic Data Structure
- [HDU 5929] Basic Data Structure (脑洞题)
- hdu 5929 Basic Data Structure(山东CCPC)
- hdu 5929 Basic Data Structure 模拟
- HDU 5929 Basic Data Structure (模拟)
- HDU5929 Basic Data Structure(双端队列)
- Atitit.软件与编程语言中的锁机制原理attilax总结
- inode 详解
- 如何为MyEclipse添加SVN插件
- solr中只查询某一个字段的设置与设置时间排序
- stm32模拟iic——引脚配置、代码
- Basic Data Structure
- mini2440 看门狗实验
- 安卓应用的热补丁动态修复技术
- Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针
- 恶意代码--adobe启发式开源检测病毒引擎技术学习分享
- js常用校验
- HTML 5 从入门到精通-中文学习教程
- 能够提高开发效率的Eclipse实用操作(2)
- error: insufficient permissions for device