杭电OJ1022~火车进站出站&南阳OJ~括号配对

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25823    Accepted Submission(s): 9754


Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
杭电OJ1022~火车进站出站&南阳OJ~括号配对_第1张图片 杭电OJ1022~火车进站出站&南阳OJ~括号配对_第2张图片 杭电OJ1022~火车进站出站&南阳OJ~括号配对_第3张图片
 

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 

Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 

Sample Input
   
   
   
   
3 123 321 3 123 312
 

Sample Output
   
   
   
   
Yes. in in in out out out FINISH No. FINISH
Hint
Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".
 


栈其实也就是一个容器而已~放一个又一个~当又要放的那个和栈最上面的那个相对应的时候,栈最上面的那个就跳出~直到栈空为止~~~

<span style="font-size:18px;">#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	int k,i,n,j;
	char a[10],b[10],v[10];
	while(~scanf("%d%s%s",&n,a,b))
	{
		i=k=j=0;
		stack<char>s;
		while(!s.empty())
			s.pop();
		while(i<n)
		{
			s.push(a[i++]);
			v[k++]=1;
			while(!s.empty()&&s.top()==b[j])
			{
				s.pop();
				j++;
				v[k++]=0;
			}
		}	
		if(!s.empty())
			printf("No.\n");
		else
		{
			printf("Yes.\n");
			for(i=0;i<k;i++)
			{
				if(v[i])
					printf("in\n");
				else
					printf("out\n");
			}	
		}
		printf("FINISH\n");
	}
	return 0;
}</span>


还有南阳OJ的括号配对问题也用到了栈~~~

括号配对问题
时间限制:3000 ms  |  内存限制:65535 KB
难度:3

描述

现在,有一行括号序列,请你检查这行括号是否配对。

输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

样例输入
3
[(])
(])
([[]()])


样例输出
No
No
Yes

代码如下~

<span style="font-size:18px;">#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;
char a[11000];
int main()
{
	int n;
	int i,j;
	int len;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		gets(a);
		len=strlen(a);
		if(len%2==1)
		    printf("No\n");
		else
		{
			if(a[0]==']'||a[0]==')')
			    printf("No\n");
			else
			{
				stack<char>s;//栈定义初始化 
		        s.push(a[0]);
		        for(i=1;i<len;i++)
		        {
		        	if(s.size()==0)
		        	    s.push(a[i]);
		        	//要先判断栈里面有没有数据,要不然s.top()会出现错误 
		        	else
					{
					    if(s.top()=='['&&a[i]==']')
		        	        s.pop();
		        	    else if(s.top()=='('&&a[i]==')')
		        	        s.pop();
		        	    else
		        	        s.push(a[i]);
		        	}
		        }
		        if(s.empty())
		            printf("Yes\n");
		        else
		            printf("No\n");
		    }
		}
	}
	return 0;
}</span>


针对这道题还有一种简便方法~虽然不用栈~但和栈的思想差不多~~便于理解~~

<span style="font-size:18px;">#include<stdio.h>
#include<string.h>
using namespace std;
char stack[11000],c[11000];
int main()
{
	int n,top,i;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		top=1;
		memset(stack,0,sizeof(stack));
		gets(stack);
		c[0]=stack[0];
		if(strlen(stack)%2==1)
			printf("No\n");
		else if(stack[0]==']'||stack[0]==')')
			printf("No\n");
		else
		{
			for(i=1;i<strlen(stack);i++)
			{
				c[top]=stack[i];
				if(c[top-1]=='['&&c[top]==']')
					top--;
				else if(c[top-1]=='('&&c[top]==')')
					top--;
				else	
					top++;
			}
			if(top==0)
				printf("Yes\n");
			else 
				printf("No\n");
		}
	}
	return 0;
}</span>


 

你可能感兴趣的:(C语言,栈,杭电,火车进站)