博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
题目26:括号匹配问题
阅读量:4652 次
发布时间:2019-06-09

本文共 1634 字,大约阅读时间需要 5 分钟。

http://ac.jobdu.com/problem.php?cid=1040&pid=25

题目描述:

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

输入:

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

    注意:cin.getline(str,100)最多只能输入99个字符!

输出:

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

样例输入:
)(rttyy())sss)(
样例输出:
)(rttyy())sss)(?            ?$
 
// 题目26:括号匹配问题.cpp: 主项目文件。#include "stdafx.h"#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int N=103;int flag[N];typedef struct Node{ int index; char ch;}Node;int main(){ //freopen("F:\\test.txt","r",stdin); //freopen("F:\\output.txt","w",stdout); char str[N]; while(scanf("%s",str)!=EOF) { printf("%s\n",str); memset(flag,0,sizeof(flag)); stack
S; for(int i=0;str[i];i++) { if(str[i]=='(') { Node temp; temp.index=i; temp.ch='('; S.push(temp); } if(str[i]==')') { if(S.empty()) { Node temp; temp.index=i; temp.ch=')'; S.push(temp); continue; } Node topTemp=S.top(); if(topTemp.ch=='(') { S.pop(); } else { Node temp; temp.index=i; temp.ch=')'; S.push(temp); } } } while(!S.empty()) { Node temp=S.top(); if(temp.ch=='(') flag[temp.index]=1; if(temp.ch==')') flag[temp.index]=2; S.pop(); } for(int i=0;str[i];i++) { if(flag[i]==0) printf(" "); else if(flag[i]==1) printf("$"); else printf("?"); } printf("\n"); } return 0;}

转载于:https://www.cnblogs.com/cjweffort/archive/2013/03/05/3374915.html

你可能感兴趣的文章
一、初识java
查看>>
动态内存
查看>>
py12 正则表达式 re模块
查看>>
OpenCV(二)
查看>>
css中position为absolute时的宽高为百分比时的参考对象问题
查看>>
C博客作业--指针
查看>>
TCP/IP详解 卷一(第十九章 TCP的交互数据流)
查看>>
DRF 权限 频率
查看>>
端口偷窃(Port Stealing)技术
查看>>
你读到了什么:谈谈阅读的空与实
查看>>
网站安全认证系统的设计变迁
查看>>
C#中泛型容器Stack<T>
查看>>
CSS兼容性
查看>>
Django反正解析路由
查看>>
《Pro Android Graphics》读书笔记之第六节
查看>>
MongoDB索引使用
查看>>
第六周
查看>>
操作系统实验2-作业调度1.0
查看>>
高质量程序设计指南c++/c语言(16)--回车和换行
查看>>
sdut 2168 Mathmen 优先队列处理区间问题
查看>>