NYOJ 2 括弧配對問題(棧)

來源:互聯網
上載者:User

http://acm.nyist.net/JudgeOnline/problem.php?pid=2

 

1、My Code:

 #include <iostream>#include <cstring>using namespace std;#define MAXSIZE 10005int main(void){int k;cin >> k;while(k--){char str[MAXSIZE];cin >> str;char stack[MAXSIZE];int len = strlen(str);int j = 0;bool tag = true;for(int i = 0; i < len; i++){switch(str[i]){case '(' :case '[' : stack[j++] = str[i];break;case ')' :if( stack[j - 1] == '(' ){tag = true;j--;}else tag = false;break;case ']' :if( stack[j - 1] == '[' ){tag = true;j--;}else tag = false;break;default : cout << "Error!" << endl;}if(tag == false)//NOTEbreak;}if( tag == true && j == 0 )cout << "Yes" << endl;elsecout << "No" << endl;}return 0;}        

 

2、時間,記憶體,長度最優代碼:

 #include<stdio.h>int main(){int t;char c,s[10001],*p;scanf("%d\n",&t);while(t--){*s=getchar();p=s+1;while((c=getchar())!='\n'){if(*(p-1)==c-1||*(p-1)==c-2)p--;else*p++=c;}if(p==s)printf("Yes\n");elseprintf("No\n");}}        

3、標程:

 #include<iostream>#include<vector>#include<string>using namespace std;int main(){int n;cin>>n;while(n--){vector<char> vec;string ch;vec.push_back(' ');cin>>ch;for(int i=0;i<ch.length();i++){vec.push_back(ch[i]);    if( vec.back()-1 == *(vec.end()-2) || vec.back()-2 == *(vec.end()-2)){vec.pop_back();vec.pop_back();}}if(vec.size()==1)cout<<"Yes"<<endl;elsecout<<"No"<<endl;}return 0;}        

 

 

(我用棧的代碼提交總是WA,不知道為什麼,見 

http://blog.csdn.net/justme0/article/details/7415337 )

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.