hdu 栈题1022&1702

http://acm.hdu.edu.cn/showproblem.php?pid=1022

http://blog.csdn.net/swm8023/article/details/6902426此处分类题

hdu1022题copy代码

 

  1. #include<iostream>  

  2. #include<stack>  

  3. #define max 100  

  4. using namespace std;  

  5. int main()  

  6. {  

  7.     stack<char>s;  

  8.     int n,i,j,k,result[max];//n为列车个数, result数组用来表示结果,1表示进栈。0表示出  

  9.     char str1[max],str2[max];//序列1和序列2  

  10.     while(cin>>n>>str1>>str2)  

  11.     {  

  12.         j=0,i=0,k=1;  

  13.         s.push(str1[0]);//为防止栈空,压一个进去  

  14.         result[0]=1;//记录进来了一个。  

  15.         while(i<n&&j<n)  

  16.         {  

  17.             if(s.size()&&s.top()==str2[j])  

  18.             {//如果栈顶元素与序列2当前的元素相等,则弹栈,序列2集团向后移一位。  

  19.                 j++;  

  20.                 s.pop();  

  21.                 result[k++]=0;  

  22.             }  

  23.             else  

  24.             {//否则从序列1中取当前元素压入栈中。  

  25.                 if(i==n)break;  

  26.                 s.push(str1[++i]);  

  27.                 result[k++]=1;  

  28.             }  

  29.         }  

  30.         if(i==n)//如果I==N表示栈顶元素不等于序列2当前元素,且序列1中元素都已经入过栈,判断不能得到序列2一样的答案。  

  31.             cout<<"No."<<endl;  

  32.         else  

  33.         {//输出进出栈方式  

  34.             cout<<"Yes."<<endl;  

  35.             for(i=0; i<k; i++)  

  36.                 if(result[i])  

  37.                     cout<<"in"<<endl;  

  38.                 else  

  39.                     cout<<"out"<<endl;  

  40.         }  

  41.         cout<<"FINISH"<<endl;  

  42.     }  

  43.     return 0;  

  44. }

 

 

 hdu1072http://acm.hdu.edu.cn/showproblem.php?pid=1702

#include <stdio.h>
#include <stack>
#include <queue>
#include <string.h>
using namespace std;
int main()
{
char s[6],op[4];

int t,num,n;

scanf("%d",&t);

while(t--)

{

scanf("%d%s",&n,s);

if(s[2]=='F')

{

queue<int> q;

while(n--)

{


scanf("%s",op);

if(op[0]=='I')

{

scanf("%d",&num);

q.push(num);

}

else

{

if(q.empty())

 

puts("None");

 

else 

{

printf("%d
",q.front()); 

q.pop();

} 

} 

} 

} 

else

{ 

stack<int> s;

while(n--)

{ 

scanf("%s",op); 

if(op[0]=='I')

{

scanf("%d",&num);

s.push(num);

}

else

{ 

if(s.empty()) 

puts("None");

else

{ 

printf("%d
",s.top());

s.pop();

}

}

} 

}

 

}

 

}

 
View Code
原文地址:https://www.cnblogs.com/ccccnzb/p/3351312.html