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 )