测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A45685. 扩号匹配问题在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。

填空题 困难

题目描述

扩号匹配问题

在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.

输入

输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母, 字符串长度不超过100

输出

对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。

样例输入

((ABCD(x)

)(rttyy())sss)(

样例输出

((ABCD(x)

$$

)(rttyy())sss)(

?               ?$

参考答案

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=1000000007; const int N=10000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; char a[101],b[101]; stack<int> S; int main() { while(cin>>a) { int len=strlen(a); for(int i=0; i<len; i++) { if(a[i]=='(') { S.push(i); b[i]=' '; } else if(a[i]==')') { if(!S.empty()) { //栈不为空 S.pop(); b[i]=' '; } else b[i]='?'; } else b[i]=' '; } while(!S.empty()) { //栈不为空 b[S.top()]='$'; S.pop(); } b[len]='\0'; cout<<a<<endl; cout<<b<<endl; } return 0; }
上一题 下一题