uva514(经典题目)
入门经典上有题解,但是代码本身其实并不能AC,需要修改,黑书上有非常深入的分析,可以参考
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<string> 6 #include<vector> 7 #include<stack> 8 #include<algorithm> 9 #include<map> 10 using namespace std; 11 const int maxn=1000+10; 12 int target[maxn]; 13 int n; 14 int main() 15 { 16 while(cin>>n&&n) 17 { 18 while(cin>>target[1]&&target[1]) 19 { 20 for(int i=2;i<=n;i++) 21 cin>>target[i]; 22 stack <int> s; 23 int A=1,B=1; 24 int ok=1; 25 while(B<=n) 26 { 27 if(A==target[B]) //进站马上出战 28 { 29 A++; B++; 30 } 31 else if(!s.empty()&&s.top()==target[B]) //站前面有车是可以先出战的 32 { 33 s.pop(); 34 B++; 35 } 36 else if(A<=n) s.push(A++); 37 else 38 { 39 ok=0; break; 40 } 41 42 } 43 cout << (ok ? "Yes" : "No" )<<endl; //注意这里必须括号括起来 44 } 45 cout <<endl; 46 } 47 return 0; 48 }
南阳理工2 (经典题目)
链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=2
用栈实现,数据结构的经典基础题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<stack> 7 #include<algorithm> 8 #include<vector> 9 #include<map> 10 using namespace std; 11 string s; 12 int main() 13 { 14 int T; 15 cin>>T; 16 for(int cas=1;cas<=T;cas++) 17 { 18 cin>>s; 19 stack<char> a; 20 int n=s.length(); 21 int ptr1=']'-'['; 22 int ptr2=')'-'('; 23 for(int i=0;i<n;i++) 24 { 25 if(a.empty()) 26 { 27 a.push(s[i]); 28 continue; 29 } 30 char t=a.top(); 31 if(s[i]-t==ptr1||s[i]-t==ptr2) 32 a.pop(); 33 else a.push(s[i]); 34 } 35 if(a.empty()) 36 cout<<"Yes"<<endl; 37 else 38 cout<<"No"<<endl; 39 } 40 return 0; 41 }