HDU-1023-Train Problem I

Train Problem II

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


Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
 

Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
 

Output
For each test case, you should output how many ways that all the trains can get out of the railway.
 

Sample Input
 
  
12310
 

Sample Output
 
  
12516796
Hint
The result will be very large, so you may not process it by 32-bit integers.
 

Author
Ignatius.L

思路:都写在代码注释上了

代码:

#include<iostream>
#include<stack>
#include<cstdio>
#include<string.h>
using namespace std;
int vis[50];
char str1[50],str2[50];
int main()
{
	int n,i,j,s,vis[50];
	while(~scanf("%d %s%s",&n,str1,str2)){
		//cout<<str1<<endl<<str2<<endl;
		stack<char>st;//选择在此处写一个stack,则可以不用进行清空,亲测可以节省时间 
		memset(vis,0,sizeof(vis));
		j=s=0;
		for(i=0;i<n;i++){
			st.push(str1[i]);
			vis[s++]=1;
			while(!st.empty()&&st.top()==str2[j]){//如果栈顶元素和字符串2的首元素相等,则将栈顶元素抛出 
				vis[s++]=0;                       //同时字符串2后移一位(字符串2对应的下标只能往后移动)
				st.pop();
				j++;
			}
		}
		if(j==n){
			printf("Yes.\n");
			for(i=0;i<s;i++){
				if(vis[i]) printf("in\n");
				else printf("out\n");
			}
		}
		else{
			printf("No.\n");
		}
		printf("FINISH\n");
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/dadaguai001/article/details/81056071