http://acm.nyist.net/JudgeOnline/problem.php?pid=2
1、标准代码:
#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;
unsigned int i=0;
for(;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;
else
cout<<"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");
else
printf("No\n");
}
}
3、网友代码:
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSIZE 10005
int 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) // NOTE
break;
}
if( tag == true && j == 0 )
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
4、我的代码:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main(int argc, char* argv[] )
{
int a;
string str;
cin>>a;
while (a--) {
cin>>str;
unsigned int len=str.length();
if(len % 2==1)
{
cout<<"No"<<endl;
}else{
unsigned int i;
stack<char> data;
for(i = 0; i < len; i++) {
if(data.empty())
{
data.push(str[i]);
}else if(data.top()=='[' && str[i]==']')
{
data.pop();
}else if(data.top()=='(' && str[i]==')')
{
data.pop();
}else if(data.top()=='{' && str[i]=='}')
{
data.pop();
}else{
data.push(str[i]);
}
}
if(data.empty())
{
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}
5、原理解析版
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//原理解析版
void printfdata(vector<char> data)
{
unsigned int i=0;
for (i=0;i<data.size();i++)
{
cout<<data.at(i)<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<'('-0<<endl;
cout<<')'-0<<endl;
cout<<'['-0<<endl;
cout<<']'-0<<endl;
cout<<'{'-0<<endl;
cout<<'}'-0<<endl;
cin>>n;
while(n--)
{
vector<char> vec;
string ch;
vec.push_back(' ');
cin>>ch;
unsigned int i=0;
for(;i<ch.length();i++)
{
vec.push_back(ch[i]);//压入栈中
if( vec.back()-1 == *(vec.end()-2) || vec.back()-2 == *(vec.end()-2))//{[()]} ascii相差1 或2
{
//cout<<"开始处理 back-1="<<(char)(vec.back()-1)<<",back-2="<<(char)(vec.back()-2)<<endl;
//printfdata(vec);
vec.pop_back();
vec.pop_back();
//printfdata(vec);
}
}
if(vec.size()==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
6、Java标准版
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int a;
a = cin.nextInt();
while(a>0)
{
String str=cin.next();
if(str.length() % 2==1)
{
System.out.println("No");
}else{
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<str.length();i++)
{
if(stack.isEmpty())
{
stack.push(str.charAt(i));
}else if(stack.peek()=='[' && str.charAt(i)== ']')
{
stack.pop();
}else if(stack.peek()=='(' && str.charAt(i)== ')')
{
stack.pop();
}else if(stack.peek()=='{' && str.charAt(i)== '}')
{
stack.pop();
}else{
stack.push(str.charAt(i));
}
}
if(stack.isEmpty())
{
System.out.println("Yes");
}else{
System.out.println("No");
}
}
a--;
}
cin.close();
}
}