【poj】1363-Rails
2012-02-13 21:03
274 查看
#include <iostream> #include <stdio.h> #include <memory.h> const int MAXSIZE = 10000; using namespace std; int trains[MAXSIZE]; int orders[MAXSIZE]; int stacks[MAXSIZE]; int top; int main() { // freopen("b:\\acm\\input.txt","r",stdin); int n,m,mount; bool bFin = true; bool bEnd = false; bool bFinput = true; while( scanf("%d",&mount) != EOF ) { bool bFound = true; int j = 0; memset(trains,MAXSIZE,0); memset(orders,MAXSIZE,0); memset(stacks,MAXSIZE,0); top = 0; if( mount == 0) { bFin = true; if(bEnd) return 0; else bEnd = true; // cout << endl; continue; } bEnd = false; if(bFin) { if(bFinput) { bFinput = false; } else { cout << endl; } n = mount;; for(int i = 0; i < n ; i++) { trains[i] = i+1; cin >> orders[i]; } bFin = false; } else { orders[0] = mount; trains[0] = 1; for(int i = 1 ; i < n; i++) { trains[i] = i+1; cin >> orders[i]; } } //trains 1 2 3 4 5 6... //orders 6 5 4 3 2 1... for(int i = 0 ; i < n ; i++) { //思路: //检查trains和stack中元素,如果 //trains中没有元素与orders[i]相等 //并且stack[top] != orers[i],No j = 0; /////This causes wrong answer if(orders[i] == stacks[top-1]) { stacks[top - 1] = 0; top--; if(i != n-1) continue; else { cout << "Yes" << endl; continue; } } while( (orders[i] != trains[j]) && (j < n) ) { if(trains[j] != 0) stacks[top++] = trains[j]; trains[j] = 0; j++; } if( (stacks[top-1] != orders[i]) && (trains[j] != orders[i])) { cout << "No" << endl; break; } else { if(stacks[top-1] == orders[i]) { stacks[top-1] = 0; top--; } if(trains[j] == orders[i]) { trains[j] = 0; } if(i == n-1) cout << "Yes" << endl; } } } return 0; }
题目地址:
http://poj.grids.cn/practice/1363/
相关文章推荐
- poj 1363 Rails
- Rails(poj 1363 栈的应用)
- Poj-1363-Rails-栈
- POJ1363 Rails【stack】【栈】
- poj 1363 Rails(栈)
- POJ-1363 Rails
- 栈,汽车进站出站:poj 1363 rails
- Rails(poj 1363 栈的应用)
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
- POJ 1363 Rails
- 【Poj】-1363-Rails(栈)
- poj 1363 Rails 【栈】
- POJ 1363 Rails 判断出栈序列是否合法
- 【POJ】1363 - Rails(栈)
- Rails--POJ 1363
- POJ - 1363 - Rails(栈)
- 【ACM】poj_1363_Rails_201308081502
- OpenJudg / Poj 1363 Rails
- POJ 1363 Rails
- poj 1363 Rails(STL应用)