数据结构与算法实验3(栈) 括号匹配
用栈ADT应用:对称符号匹配判断
输入一行符号,以#结束,判断其中的对称符号是否匹配。对称符号包括:
{ } 、 [ ] 、 ( )、 < >
输出分为以下几种情况:
(1)对称符号都匹配,输出 “ right. “
(2) 如果处理到最后出现了失配,则输出两行:
第一行:Matching failure.
第二行:loss of right character $$…. 其中$$… 是按嵌套顺序对应的右匹配符号。
(3)处理到某个符号时失配了,则输出两行或三行:
第一行: The N character ‘$’ is wrong.” ,其中N是出错符号的序号,$是出错的符号;
第二行: loss of left character $.” 其中 $ 是当前符号的左匹配符号。
(如果有的话)第三行:loss of right character $$…” 其中$$… 是按嵌套顺序对应的右匹配符号。
例如:
输入
(a.b)>#
输出:
The 6 character >’ is wrong.
loss of left character <.
输入 :
({()#
输出:
Matching failure.
loss of right character }).
例如:
输入 | Result |
---|---|
| |
| |
ps:简单的栈应用——代码是不是易懂呢。。
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
#define rep(i , a , b) for(register int i=(a);i<=(b);++i)
#define per(i , a , b) for(register int i=(a);i>=(b);--i)
// #define ms(s) memset(s, 0, sizeof(s))
// typedef long long ll;
// typedef unsigned long long ull;
// typedef pair<int , int> pi;
// typedef unordered_map<int,int> un_map;
template<class T>
inline void read (T &x) {
x = 0;
int sign = 1;
char c = getchar ();
while (c < '0' || c > '9') {
if ( c == '-' ) sign = - 1;
c = getchar ();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar ();
}
x = x * sign;
}
const int maxn = 1e5+10;
// const int inf = 0x3f3f3f3f;
// const ll INF = ll(1e18);
// const ll mod = 1011110011;
// const double PI = acos(-1);
char s[maxn];
stack<int>p,q,r,m;
void print_s() {
if(p.size()==1&&q.size()==1&&r.size()==1&&m.size()==1) return;
printf("loss of right character ");
while(p.size()>1||q.size()>1||r.size()>1||m.size()>1) {
int x=p.top();
int y=q.top();
int z=r.top();
int l=m.top();
if(x>y&&x>z&&x>l) {
p.pop();
printf(")");
}
if(y>x&&y>z&&y>l) {
q.pop();
printf("]");
}
if(z>x&&z>y&&z>l) {
r.pop();
printf("}");
}
if(l>x&&l>y&&l>z) {
m.pop();
printf(">");
}
}
printf(".\n");
}
int main(int argc, char * argv[])
{
scanf("%s",s+1);
int n = strlen(s+1);
int f=0;
p.push(-1);
q.push(-1);
r.push(-1);
m.push(-1);
rep(i,1,n) {
if(s[i]=='(') p.push(i);
else if(s[i]=='[') q.push(i);
else if(s[i]=='{') r.push(i);
else if(s[i]=='<') m.push(i);
else if(s[i]==')') {
int x = p.top();
int y = q.top();
int z = r.top();
int l = m.top();
if(x>y&&x>z&&x>l) p.pop();
else {
printf("The %d character '%c' is wrong.\n",i,s[i]);
printf("loss of left character (.\n");
f=1;
break;
}
}
else if(s[i]==']') {
int x = p.top();
int y = q.top();
int z = r.top();
int l = m.top();
if(y>x&&y>z&&y>l) q.pop();
else {
printf("The %d character '%c' is wrong.\n",i,s[i]);
printf("loss of left character [.\n");
f=1;
break;
}
}
else if(s[i]=='}') {
int x = p.top();
int y = q.top();
int z = r.top();
int l = m.top();
if(z>y&&z>x&&z>l) r.pop();
else {
printf("The %d character '%c' is wrong.\n",i,s[i]);
printf("loss of left character {.\n");
f=1;
break;
}
}
else if(s[i]=='>'){
int x = p.top();
int y = q.top();
int z = r.top();
int l = m.top();
if(l>y&&l>x&&l>y) m.pop();
else {
printf("The %d character '%c' is wrong.\n",i,s[i]);
printf("loss of left character <.\n");
f=1;
break;
}
}
}
if(f) {
print_s();
return 0;
}
if(p.size()==1&&q.size()==1&&r.size()==1&&m.size()==1) {
printf("right.\n");
}
else {
printf("Matching failure.\n");
print_s();
}
return 0;
}