zoukankan      html  css  js  c++  java
  • [Water]Hdu 1022 Train Problem I

    声明一个char类型的栈。

    假设题目输入内容为n(int),a(string),b(string),a、b的下标记为index1和indexe2。

    第一步肯定是入栈。

    然后遍历b中每个字符:
      如果栈不为空且栈顶元素等于b[index2],出栈。否则a[index1]入栈,如果没有元素可入栈,输出No。

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    
    using namespace std;
    
    int n;
    string a,b;
    string ans;
    
    stack<char>s;
    queue<int>q;
    
    int main(){
    	while(cin>>n>>a>>b){
    
    		int index1=1,index2=0;
    		bool ok=true;
    
    		while(!q.empty())q.pop();
    		while(!s.empty())s.pop();
    
    		q.push(1);
    		s.push(a[0]);
    
    		while(index2<n){
    			if(s.empty()){
    				if(index1==n){
    					ok=false;
    					break;
    				}
    
    				q.push(1);
    				s.push(a[index1]);
    				index1++;
    			}
    			if(s.top()==b[index2]){
    				q.push(0);
    				s.pop();
    				index2++;
    			}else{
    				if(index1==n){
    					ok=false;
    					break;
    				}
    
    				q.push(1);
    				s.push(a[index1]);
    				index1++;
    			}
    		}
    
    		if(ok){
    			cout<<"Yes."<<endl;
    			while(!q.empty()){
    				if(q.front()==1)cout<<"in"<<endl;
    				else cout<<"out"<<endl;
    				q.pop();
    			}
    		}
    		else cout<<"No."<<endl;
    		cout<<"FINISH"<<endl;
    	}
    }
    

      

  • 相关阅读:
    CSS学习1
    三个和尚没水喝阅读笔记
    Javascript学习1

    mv 批量
    emacs 大小写转换
    too many open files
    成都定房
    有关重定向
    postgresql 数据库
  • 原文地址:https://www.cnblogs.com/bruce27/p/4549977.html
Copyright © 2011-2022 走看看