1203:扩号匹配问题

只谈情不闲聊 提交于 2020-01-28 01:35:19

 

【题目描述】

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

【输入】

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

【输出】

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

【输入样例】

((ABCD(x)
)(rttyy())sss)(

【输出样例】

((ABCD(x)
$$
)(rttyy())sss)(
?            ?$
#if(1)

/*#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <stack>*/
#include <bits/stdc++.h>

#define A 1000+5

using namespace std;

const int maxn=100+5;
int ans=0;
char ch[maxn],a[maxn];
int i,j;

/*inline void caculate()
{
    if(ch[0]==)
}*/


int main()
{
    stack<int>s;

    while(cin>>ch)
    {
        int len=strlen(ch);
        for(i=0;i<len;i++)
        {
            if(ch[i]=='(')
            {
                s.push(i);
                a[i]=' ';
            }

            else if(ch[i]==')')
            {
                if(!s.empty())
                {
                    s.pop();
                    a[i]=' ';
                }

                else a[i]='?';
            }

            else a[i]=' ';//对剩余的字母操作

        }

        while (!s.empty())   //重新遍历栈内元素(所以要用int型的栈)
        {
            a[s.top()]='$';
            s.pop();
        }

        for(i=0;i<len;i++)
        {
            cout<<ch[i];
        }
        
        cout<<endl;
        
        for(i=0;i<len;i++)
        {
            cout<<a[i];
        }
        
        cout<<endl;
    }

}


#endif

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!