题目描述:

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

输入:

输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100。
    注意:cin.getline(str,100)最多只能输入99个字符!

输出:

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

样例输入:
)(rttyy())sss)(
样例输出:
)(rttyy())sss)(
? ?$ 开始用两个数组记录
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> #define MAX 102
char temp[MAX];
int left[MAX];
int right[MAX]; int main(int argc, char const *argv[])
{
freopen("input.txt","r",stdin);
while(scanf("%s",temp) != EOF) {
memset(left,,sizeof(left));
memset(right,,sizeof(right)); int lcnt = , rcnt = ;
int len = strlen(temp);
for(int i = ; i < len; i++) {
if(temp[i] == '(') {
lcnt++;
}
else if(temp[i] == ')') {
lcnt--;
if(lcnt < ) {
left[i] = ;
lcnt = ;
}
}
}
for(int i = len-; i >= ; i--) {
if(temp[i] == ')') {
rcnt++;
}
else if(temp[i] == '(') {
rcnt--;
if(rcnt < ) {
right[i] = ;
rcnt = ;
}
}
}
puts(temp);
for(int i = ; i < len; i++) {
if(left[i] == ) {
printf("?");
}
if(right[i] == ) {
printf("$");
}
if(left[i] == && right[i] == ) {
printf(" ");
}
}
puts("");
}
return ;
}

也可以改成一个

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> #define MAX 102
char temp[MAX];
int flag[MAX]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%s",temp) != EOF) {
memset(flag,,sizeof(flag)); int lcnt = , rcnt = ;
int len = strlen(temp);
for(int i = ; i < len; i++) {
if(temp[i] == '(') {
lcnt++;
}
else if(temp[i] == ')') {
lcnt--;
if(lcnt < ) {
flag[i] = ;
lcnt = ;
}
}
}
for(int i = len-; i >= ; i--) {
if(temp[i] == ')') {
rcnt++;
}
else if(temp[i] == '(') {
rcnt--;
if(rcnt < ) {
flag[i] = ;
rcnt = ;
}
}
}
puts(temp);
for(int i = ; i < len; i++) {
if(flag[i] == && temp[i] == ')') {
printf("?");
}
else if(flag[i] == && temp[i] == '(') {
printf("$");
}
if(flag[i]== ) {
printf(" ");
}
}
puts("");
}
return ;
}

还可以不要这个数组,改为一个char数组

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> #define MAX 102
char temp[MAX];
char temp2[MAX]; int main(int argc, char const *argv[])
{
freopen("input.txt","r",stdin);
while(scanf("%s",temp) != EOF) {
puts(temp);
int lcnt = , rcnt = ;
int len = strlen(temp);
temp2[len] = '\0';
for(int i = ; i < len; i++) {
if(temp[i] == '(') {
lcnt++;
temp2[i] = ' ';
}
else if(temp[i] == ')') {
lcnt--;
if(lcnt < ) {
temp2[i] = '?';
lcnt = ;
}
}
else {
temp2[i] = ' ';
}
}
for(int i = len-; i >= ; i--) {
if(temp[i] == ')') {
rcnt++;
if(temp2[i] != '?') {
temp2[i] = ' ';
} }
else if(temp[i] == '(') {
rcnt--;
if(rcnt < ) {
temp2[i] = '$';
rcnt = ;
}
}
else {
temp2[i] = ' ';
}
} puts(temp2);
}
return ;
}

九度oj 题目1153:括号匹配问题的更多相关文章

  1. 九度oj题目1153:括号匹配问题

    题目1153:括号匹配问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4866 解决:2112 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见 ...

  2. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  3. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  5. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  6. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  7. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  8. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  9. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

随机推荐

  1. Java 实现Excel表数据的读取和写入 以及过程中可能遇到的问题

    问题1:Unable to recognize OLE stream 格式的问题要可能是因为给的数据是2010年的数据表后缀为.xlsx,要先转化成2003版的后缀为.xls 问题2: Warning ...

  2. jquery.qrcode.min.js生成二维码

    jquery.qrcode.min.js是一款可以生成二维码的插件,使用前提是先引入jquery,因为jquery.qrcode.min.js依赖jquery. 基本用法 1.引入js <scr ...

  3. 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期

    深入探讨 Scope 作用域 每一个 $scope 都是类 Scope 的一个实例.类 Scope 拥有可以控制 scope 生命周期的方法,提供事件传播的能力,并支持模板渲染. 作用域的层次结构 让 ...

  4. Glide图片框架

    //加载圆形图片Glide.with(this).load(WSCAppStatic.WEB_KEFU_PHOTO_URL+ "?usercode=8120000315") .as ...

  5. Linux shell例子

    #!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...

  6. codevs 1131 统计单词数 2011年NOIP全国联赛普及组

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位 ...

  7. javascript基本类型和引用类型,作用域和内存问题

    基本类型(null.undefined.boolean.number.string)和引用类型(Object 对象) 1  基本类型:只能不存一个值,一种类型:从一个变量向另一个变量复制基本类型的值, ...

  8. osx launchpad图标的删除

    安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,可是好多都不是我自己想要的,我们该怎么删除或者修改呢,下面介绍一些方法: ①直接操作Appications ...

  9. 微擎框架中receive.php代码分析

  10. Linux系统GEDIT编译运行C++

    作为NOIP第一年强制使用Linux系统的考生,真的很难受,被迫还要学一波Linux系统. 正常的Windows对于较基础的程序员来说非常方便好用,但是对于高级程序员来说就是一个坑,于是就有了Linu ...