首页 > 代码库 > ZOJ 1259 Rails

ZOJ 1259 Rails

stack的应用

 1 #include<iostream> 2 #include<cstdio> 3 #include<stack> 4 using namespace std; 5 int goal[1005]; 6 int d[1005]; 7 stack<int>s; 8 int main() 9 {10     freopen("input.txt","r",stdin);11     int n;12     for(int i = 0; i <= 1000; i++)13         d[i] = i;14     while(cin>>n && n)15     {16         while(cin>>goal[1] && goal[1])17         {18             while(!s.empty())19                 s.pop();20             for(int i = 2; i <= n; i++)21                 cin>>goal[i];22             int i = 2;23             int pos = 1;24             s.push(d[1]);25             while(pos <= n && i <= n+1)26             {27                 if(s.empty() || s.top() != goal[pos])28                 {29                     s.push(d[i]);30                     i++;31                 }32                 else if(s.top() == goal[pos])33                 {34                     s.pop();35                     pos++;36                 }37                 else break;38             }39             if(s.empty())40                 cout<<"Yes"<<endl;41             else cout<<"No"<<endl;42 43         }44         cout<<endl;45     }46     return 0;47 }