UVA 673 Parentheses Balance
2017-09-14 11:59
453 查看
题目链接:点击打开链接
又是括号的匹配问题
还是用栈
#include<bits/stdc++.h>
using namespace std;
stack<char> sta;
int main(){
//freopen("in.txt","r",stdin);
int n;
string str;
cin>>n;
getchar();
while(n--){
while(!sta.empty()) sta.pop();
int flag=0;
getline(cin,str);
int len=str.size();
for(int i=0;i<len;i++){
if(str[i]=='['||str[i]=='(') sta.push(str[i]);
else if(!sta.empty()&&str[i]==')'&&sta.top()=='(') sta.pop();
else if(!sta.empty()&&str[i]==']'&&sta.top()=='[') sta.pop();
else flag=1;
}
if(!flag&&sta.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
又是括号的匹配问题
还是用栈
#include<bits/stdc++.h>
using namespace std;
stack<char> sta;
int main(){
//freopen("in.txt","r",stdin);
int n;
string str;
cin>>n;
getchar();
while(n--){
while(!sta.empty()) sta.pop();
int flag=0;
getline(cin,str);
int len=str.size();
for(int i=0;i<len;i++){
if(str[i]=='['||str[i]=='(') sta.push(str[i]);
else if(!sta.empty()&&str[i]==')'&&sta.top()=='(') sta.pop();
else if(!sta.empty()&&str[i]==']'&&sta.top()=='[') sta.pop();
else flag=1;
}
if(!flag&&sta.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
相关文章推荐
- Parentheses Balance UVA 673
- uva 673 Parentheses Balance
- UVA-673 Parentheses Balance
- uva 673 - Parentheses Balance
- UVA - 673 Parentheses Balance
- UVA 673 (暑假-线性表 -A - Parentheses Balance)
- UVa673 - Parentheses Balance
- UVa - 673 - Parentheses Balance(栈-stack)(Java和C++)
- UVa 673 - Parentheses Balance
- uva673 Parentheses Balance
- UVA 673 Parentheses Balance
- Uva673——Parentheses Balance
- UVa OJ Parentheses Balance 673 括号平衡
- Parentheses Balance UVA - 673
- Uva-673-Parentheses Balance
- uva 673 - Parentheses Balance
- UVA - 673 - Parentheses Balance(栈)
- Uva 673 Parentheses Balance
- uva 673 Parentheses Balance(栈)
- uva673-Parentheses Balance