HDU1022 Train Problem I (栈)
时间:2014-04-28 09:18:36
收藏:0
阅读:537
栈+队列
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&n)!=EOF) 13 { 14 memset(a,0,sizeof(a)); 15 memset(b,0,sizeof(b)); 16 scanf("%s",&a); 17 scanf("%s",&b); 18 int i=0,j=0; 19 while(!s.empty()) s.pop(); 20 while(!q.empty()) q.pop(); 21 while(i<n) 22 { 23 s.push(a[i]); 24 q.push(1); 25 while(!s.empty()&&s.top()==b[j]) 26 { 27 s.pop(); 28 q.push(0); 29 j++; 30 } 31 i++; 32 } 33 if(s.empty()) 34 { 35 printf("Yes.\n"); 36 while(!q.empty()) 37 { 38 if(q.front()) printf("in\n"); 39 else printf("out\n"); 40 q.pop(); 41 } 42 } 43 else printf("No.\n"); 44 printf("FINISH\n"); 45 } 46 }
评论(0)