/************ 1 2 3 4 如棧 出棧 3 2 4 1 返回0 錯誤 3 1 2 4 返回-1 **********/ #include<iostream> #include<stack> #include<string.h> using namespace std; bool judge(const char *push_seq, const char *pop_seq) { if (NULL == push_seq || NULL == pop_seq) { return false; } if (strlen(push_seq) != strlen(pop_seq)) { return false; } stack<char>sc; while (*push_seq) { if (0 == sc.size() || sc.top() != *pop_seq) { sc.push(*push_seq++); // } else { sc.pop(); ++pop_seq; // } } while (sc.size()) { if (sc.top() != *pop_seq++) { return false; } sc.pop(); } return true; } int mycheck1(int a[],int n) { stack<int>s; int i,j,k=1; // 3 2 4 1 for(i=0;i<n;i++) { if(a[i]>k) { for(j=k;j<a[i];j++) s.push(j); k=j+1;cout<<k<<endl; continue; } if(a[i]==s.top()){s.pop();continue;} if(a[i]<s.top()) return -1; } cout<<endl; return 0; } int mycheck(int a[],int n) { stack<int>s; s.push(0); int i,j,k=1; //BUG段 經過分析 在于s.top段 因為沒有元素時這樣做有越界的含義 for(i=0;i<n;i++) { if(a[i]>k) { for(j=k;j<a[i];j++) s.push(j); k=j+1;cout<<k<<endl; continue; } if(a[i]==s.top()){s.pop();continue;} if(a[i]<s.top()) return -1; } cout<<endl; return 0; } int main() { //cout<<judge("1234","3241")<<endl; //int a[]={1,2,3,4}; int a[]={3,2,4,1}; // int a[]={4,3,2,1};ok cout<<mycheck(a,4); return 0; }
網頁名稱:小代碼棧之合法性一點思路不一樣的際遇
網站鏈接:http://m.2m8n56k.cn/article26/jdsscg.html
成都網站建設公司_創新互聯,為您提供網站設計、微信公眾號、品牌網站制作、虛擬主機、網站設計公司、軟件開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯