SDUT 栈和队列专题
来源:互联网 发布:js plumb 编辑:程序博客网 时间:2024/05/20 22:38
#include<bits/stdc++.h>using namespace std;typedef int ElemType;#define OK 1#define FAIL -1#define MAXSIZE 100010#define SIZEINCREMENT 100010typedef struct node{ ElemType *base; int length;} Stack;Stack *init(){ Stack *s; s = new node; s->base = new ElemType[MAXSIZE]; if(!s->base) exit(FAIL); s->length = 0; return s;}int Empty(Stack *s){ if(s->length == 0) return 1; else return 0;}int Push(Stack *s, ElemType x){ if(s->length >= MAXSIZE) return 0; s->length++; s->base[s->length] = x; return 1;}int Pop(Stack *s){ if(!Empty(s)) { s->length--; return 1; } else return 0;}int Top(Stack *s){ if(!Empty(s)) return s->base[s->length]; else return -1;///返回负值}int convert(char a){ if(a == '*' || a == '/') return 2; else if(a == '+' || a == '-') return 1; else return 0;}void solve()///一般算术表达式转换成后缀式{ Stack *s; s = init(); char suf[200], in[200]; int i, j; scanf("%s", in); for(i = 0, j = 0; in[i] != '#'; i++) { if(in[i] >= 'a' && in[i] <= 'z') suf[j++] = in[i]; else if(Empty(s) || Top(s) == '(' || in[i] == '(') Push(s, in[i]); else if(in[i] == ')') { while(!Empty(s) && Top(s) != '(') { suf[j++] = Top(s); Pop(s); } if(!Empty(s)) Pop(s); } else { while((!Empty(s)) && (convert(Top(s)) >= convert(in[i]))) { suf[j++] = Top(s); Pop(s); } Push(s, in[i]); } } while(!Empty(s)) { suf[j++] = Top(s); Pop(s); } suf[j] = 0; puts(suf);}void solve1()///后缀式求值{ char a[1000]; Stack *s; s = init(); scanf("%s", a); for(int i = 0; a[i] != '#'; i++) { if(a[i] >= '0' && a[i] <= '9') Push(s, a[i] - '0'); else { int x, y; if(!Empty(s)) y = Top(s); Pop(s); if(!Empty(s)) x = Top(s); Pop(s); switch(a[i]) { case '+': x += y; Push(s, x); break; case '-': x -= y; Push(s, x); break; case '*': x *= y; Push(s, x); break; case '/': x /= y; Push(s, x); break; } } } cout << Top(s) << endl;}void solve2()///括号匹配(use gets()){ char a[100]; Stack *s; int flag; while(gets(a)) { flag = 1; s = init(); int len = strlen(a); for(int i = 0; i < len; i++) { if(a[i] == '(' || a[i] == '{' || a[i] == '[') Push(s, a[i]); else { if(a[i] == ')') { if(Top(s) == '(') Pop(s); else { flag = 0; break; } } else if(a[i] == '}') { if(Top(s) == '{') Pop(s); else { flag = 0; break; } } else if(a[i] == ']') { if(Top(s) == '[') Pop(s); else { flag = 0; break; } } } } if(Empty(s) && flag) cout << "yes" << endl; else cout << "no" << endl; }}void solve3()///栈的基本操作{ int t, m, n, k; char x; Stack *s; cin >> t; while(t--) { s = init(); cin >> m >> n; getchar(); for(int i = 0; i < n; i++) { cin >> x; switch(x) { case 'P':{ cin >> k; if(s->length >= m) cout << 'F' << endl; else { getchar(); Push(s, k); } break;} case 'O': if(Empty(s)) cout << 'E' << endl; else { cout << Top(s) << endl; Pop(s); } break; case 'A': if(Empty(s)) cout << 'E' << endl; else { cout << Top(s) << endl; } break; } } cout << endl; }}void solve4()///行编辑器{ Stack *s; char a[1000]; int i = 0; while(gets(a)) { s = init(); for(i = 0; a[i] != 0; i++) { if(a[i] == '#') { Pop(s); } else if(a[i] == '@') { s->length = 0; } else Push(s, a[i]); } i = 1; while(s->length >= i) printf("%c", s->base[i++]); printf("\n"); }}void solve5()///出栈序列判定(Don't use char as the ElemType){ Stack *s; int a[10100], b[10100], i, j, n, t; scanf("%d", &n); for(i = 0; i < n; i++) cin >> a[i]; cin >> t; while(t--) { for(i = 0; i < n; i++) cin >> b[i]; s = init(); j = 0; for(i = 0; i < n; i++) { Push(s, a[i]); while(!Empty(s) && Top(s) == b[j]) { Pop(s); j++; } } if(j == n) cout << "yes" << endl; else cout << "no" << endl; }}///进制转换void solve6(Stack *s)///这里注意要判断零的情况{ int n, m; cin >> n >> m; if(n == 0) printf("0\n"); else { while(n) { Push(s, n % m); n /= m; } while(!Empty(s)) { printf("%d", Top(s)); if(!Empty(s)) Pop(s); } printf("\n"); }}int main(){ solve5(); return 0;}
阅读全文
0 0
- SDUT 栈和队列专题
- 栈和队列专题
- SDUT 2088 (STL 栈和队列)
- SDUT -refresh的停车场(栈和队列)
- [SDUT](2088)refresh的停车场 ---栈和队列
- 栈与队列专题
- 《剑指offer》之链表、栈和队列专题
- SDUT 链表专题
- sdut oj2088 refresh 的停车场(栈和队列的混合应用)
- SDUT 双向队列(队列)
- SDUT 1466 双向队列
- 双向队列 SDUT 1466
- SDUT 1466 双向队列
- SDUT 1466 双向队列
- sdut oj1466 双向队列
- acm sdut 双向队列
- SDUT-双向队列
- SDUT-1466 双向队列
- 控制反转(IOC)和依赖注入(DI)的区别
- PullToRefresh加载及刷新
- 基础练习 十六进制转八进制
- Robotframework + Ride + python在ubuntu 14下搭建自动化测试环境
- ubuntu设置当前用户环境变量
- SDUT 栈和队列专题
- 习题1
- java泛形之坑
- 不重叠线段 51Nod(排序+贪心) <===>HDU 今年暑假不AC
- html节点树分享(html元素间的空格会在节点树上产生一个text节点)
- Rdds基本操作Action
- C++ map注意事项
- 一篇给力的Bag-of-words模型入门介绍
- linux mysql5.7配置主从同步