(数据结构:栈)HDU 1022-Train Problem I
来源:互联网 发布:淘宝内衣真人秀视频 编辑:程序博客网 时间:2024/06/05 20:32
原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1022
翻译:
随着新学期的到来,如今Ignatius火车站很繁忙,许多学生想要坐火车回学校(因为Ignatius火车站的火车是全世界最快的)。但是这里有个问题,在所有的火车站点上只有一个铁轨,所以,所有火车都是从一边进入,再从另一边出去。对于这个问题,如果火车A第一个进入铁轨,然后火车B在火车A前离开,一直到火车B离开,火车A都不能离开。这个图片展示了这个问题。现在在火车站上有最多有9个火车,所有的火车有一个编号(从1到n),火车以O1次序进入铁轨,你的任务是确定火车是否能以O2的次序离开。
输入包含多个测试数据。每组测试数据是由一个整数(列车的数量)和两个字符串,火车进入的次序是:O1,火车离开的次序:O2,到文件尾结束。
如果你不能交换O1和O2,你要输出一行“No”,否则你应该输出一行“Yes”,然后输出你交换次序的方式(火车入轨为”in”,火车出轨为”out”),最后打印一行“FINISH”。
用c语言写:
#include<stdio.h>#include<stdlib.h>#include<string.h>struct Node{ int num; struct Node *next;};typedef struct Node *NODE;typedef NODE Stack;Stack CreatStack();void MakeEmpty(Stack s);void push(int n, Stack s);void pop(Stack s);int top(Stack s);int isEmpty(Stack s);int main(){ int n; int a,b,c; //a指向下一次要进入的火车 //b指向下一次要出去的火车 char in[15] = { 0 }; char out[15] = { 0 }; int step[25]; //数组step和c用来记录火车的进入和出去,默认-1,进入1,出去0 while (scanf("%d %s %s", &n,in,out)!=EOF){ memset(step,-1,sizeof(step)); Stack s = CreatStack(); a = 0; b = 0; c = 0; while (a < n){ //如果栈不是空,或者栈顶不符合当前需要出去的火车吗,就进入 if (isEmpty(s) || top(s) != out[b]-'0'){ push(in[a] - '0', s); step[c++] = 1; a++; } //循环判断可以出去的 while (!isEmpty(s) && top(s) == out[b]-'0'){ pop(s); step[c++]=0; b++; } } if (a == n ){ if (isEmpty(s)){ printf("Yes.\n"); int i = 0; for(i=0;i<c;i++){ if(step[i]==1){ printf("in\n"); } else{ printf("out\n"); } } } else{ printf("No.\n"); } } printf("FINISH\n"); } return 0;}//检查栈是否为空int isEmpty(Stack s){ return s->next == NULL;}//清空栈void MakeEmpty(Stack s){ while (!isEmpty(s)){ pop(s); }}//建立栈Stack CreatStack(){ Stack s; s = malloc(sizeof(struct Node)); s->next = NULL; MakeEmpty(s); return s;}//进栈void push(int n, Stack s){ NODE new_node; new_node = malloc(sizeof(struct Node)); new_node->num = n; new_node->next = s->next; s->next = new_node;}//出栈void pop(Stack s){ NODE temp_node = s->next; s->next = temp_node->next; free(temp_node);}//返回栈顶int top(Stack s){ if (!isEmpty(s)){ return s->next->num; } return 0;}
C++STL:
#include<iostream>#include<stack>#include<cstring>using namespace std;int main(){ int n; int a,b,c; char in[15]; char out[15]; int step[25]; while(cin>>n>>in>>out){ memset(step,-1,sizeof(step)); stack<int> s; a=b=c=0; while(a<n){ if(s.empty()||s.top()!=out[b]-'0'){ s.push(in[a]-'0'); step[c++] = 1; a++; } while (!s.empty() && s.top() == out[b]-'0'){ s.pop(); step[c++]=0; b++; } } if (a == n){ if (s.empty()){ cout<<"Yes."<<endl; int i = 0; for(i=0;i<c;i++){ if(step[i]==1){ cout<<"in"<<endl; } else{ cout<<"out"<<endl; } } } else{ cout<<"No."<<endl; } } cout<<"FINISH"<<endl; } return 0;}
0 0
- HDU 1022 Train Problem I (数据结构 —— 栈)
- (数据结构:栈)HDU 1022-Train Problem I
- hdu 1022 Train Problem I(栈)
- hdu 1022 Train Problem I(栈)
- hdu 1022----Train Problem I(栈)
- HDU - 1022 - Train Problem I(栈)
- HDU 1022 Train Problem I(栈)
- hdu 1022 Train Problem I(栈)
- HDU 1022 Train Problem I(栈)
- hdu 1022 Train Problem I 栈
- Train Problem I hdu 1022 栈
- hdu 1022 Train Problem I(栈)
- hdu 1022 Train problem I(栈~~)
- HDU 1022 Train Problem I 栈、队列
- HDU 1022 Train Problem I (栈)
- HDU 1022 Train Problem I 栈
- hdu 1022 Train Problem I 栈模拟
- HDU 1022 Train Problem I【栈】
- 反射机制
- Core Animation(核心动画)的基本介绍与使用
- Android gallery画廊
- AndroidUI设计之高仿系列
- 存储过程的优缺点
- (数据结构:栈)HDU 1022-Train Problem I
- 性能测试相关指标计算法则
- 协方差的意义和计算公式
- 深入理解Java内存模型(二)——重排序
- HYSBZ/BZOJ 1004 [HNOI2008] Cards - 组合数学
- iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
- diff 比较2个文件的不同
- Linux无root权限安装mysql绿色版
- Android清除应用缓存数据