题目描述:

在某个字符串(长度不超过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. 详解ASP.NET缓存机制

    文中对ASP.NET的缓存机制进行了简述,ASP.NET中的缓存极大的简化了开发人员的使用,如果使用得当,程序性能会有客观的提升.缓存是在内存存储数据的一项技术,也是ASP.NET中提供的重要特性之一 ...

  2. AndroidStudio第一次提交项目代码到git服务器/github

    虽然使用AndroidStudio(以下简称as)开发并使用git管理代码已经有很长时间,但是第一次提交项目到git依然会很不顺利,网上的文章或许因为所使用版本比较老,并不一定完全凑效,因此写此笔记做 ...

  3. sysbench0.5安装和使用介绍

    sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况,sysbench支持MySQL.PostgreSQL.Oracle数据库OLTP测试.它 ...

  4. (十二)maven之nexus仓库的基本用法

    nexus仓库的基本用法 ① 启动nexus. 上一章有提到:https://www.cnblogs.com/NYfor2018/p/9079068.html ② 访问http://localhost ...

  5. 霍金的新语音系统 ACAT 将开源

    英国理论物理学家斯蒂芬·霍金(Stephen Hawking)使用了二十年的语音通讯系统被英特尔开发的新一代通讯平台替代,显著改进了通讯效率.但霍金的声音并没有发生改变,他仍然使用相同的语音合成器.霍 ...

  6. jquery实现跑马灯效果(一)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. k sum(lintcode)

    没通过的代码: class Solution { public: /* * @param A: An integer array * @param k: A positive integer (k & ...

  8. 快学UiAutomator UiDevice API 详解

    一.按键使用 返回值 方法名 说明 boolean pressBack() 模拟短按返回back键 boolean pressDPadCenter() 模拟按轨迹球中点按键 boolean press ...

  9. OpenCV2:第十一章 图像转换

    一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...

  10. bootstrap历练实例: 垂直胶囊式的导航菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...