欢迎光临散文网 会员登陆 & 注册

括号匹配

2023-03-21 21:23 作者:仓鼠翞  | 我要投稿

//括号匹配

#include<bits/stdc++.h>

using namespace std;

stack<char> st;

char a[10000][10000];


int main()

{

fill(a[0],a[0]+10000*10000,0);

for(int i=1;i<=6;i++)

{

scanf("%s",&a[i]);

}

for(int i=1;i<=6;i++)

{

bool judge=false;

for(int j=0;j<10000&&a[i][j]!='0';j++)

{

if(a[i][j]=='[')

{

st.push(a[i][j]);

}

else if(a[i][j]=='(')

{

st.push(a[i][j]);

}

else if(a[i][j]=='{')

{

st.push(a[i][j]);

}

else if(a[i][j]=='<')

{

st.push(a[i][j]);

}

if(a[i][j]==']')

{

char temp=st.top();

st.pop();

if(temp=='[')

{

judge=true;

}

else

{

printf("No\n");

continue;

}

}

else if(a[i][j]==')')

{

    char temp=st.top();

st.pop();

if(temp=='(')

{

judge=true;

}

else

{

printf("No\n");

continue;

}

}

else if(a[i][j]=='}')

{

char temp=st.top();

st.pop();

if(temp=='{')

{

judge=true;

}

else

{

printf("No\n");

continue;

}

}

else if(a[i][j]=='>')

{

char temp=st.top();

st.pop();

if(temp=='<')

{

judge=true;

}

    else

{

printf("No\n");

    continue;

}

}

}

if(judge==true)

{

printf("Yes\n");

}

}

return 0;

}


括号匹配的评论 (共 条)

分享到微博请遵守国家法律