九度oj 题目1153:括号匹配问题
- 题目描述:
-
在某个字符串(长度不超过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:括号匹配问题的更多相关文章
- 九度oj题目1153:括号匹配问题
题目1153:括号匹配问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4866 解决:2112 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
随机推荐
- 本机运行x程序出现:Can't open display 原因及其解决方法(貌似非永久)
http://blog.sina.com.cn/s/blog_53db572501016ma7.html 这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的 ...
- 【原创】微信公众号与HTML 5混合模式揭秘4——jssdk调用微信扫一扫
微信公众号与HTML 5混合模式揭秘1——如何部署JSSDK 微信公众号与HTML 5混合模式揭秘2——分享手机相册中照片 微信公众号与HTML 5混合模式揭秘3——JSSDK获取地理位置 在JS ...
- arcgis jsapi接口入门系列(7):鼠标在地图画线
初始化,每个map执行一次就行 drawPolylineInit: function () { //画几何对象初始化 //新建一个图形图层用于存放画图过程中的图形 let layer = new th ...
- Glide图片框架
//加载圆形图片Glide.with(this).load(WSCAppStatic.WEB_KEFU_PHOTO_URL+ "?usercode=8120000315") .as ...
- MD5 介绍
MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能: 输入任意 ...
- Azure 8 月新公布
Azure 8 月新发布:Cosmos DB 每分钟请求单位,存储的托管磁盘及促销,高级和标准磁盘存储大尺寸磁盘,高级磁盘存储小尺寸磁盘. Azure Cosmos DB:每分钟请求单位为您降低成本, ...
- Exoplanet: The hunt is on
原文 How many planets are out there? Today scientists believe that planets could outnumber the stars.F ...
- cannot load such file -- bundler/setup解决
sudo gem install bundler bundle update celluloid
- 4个Linux服务器监控工具
下面是我想呈现给你的4个强大的监控工具. htop – 交互式进程查看器 你可能知道在机器上查看实时进程的标准工具top.如果不知道,请运行$ top看看,运行$ man top阅读帮助手册. hto ...
- 从汇编看c++中临时对象的析构时机
http://www.cnblogs.com/chaoguo1234/archive/2013/05/12/3074425.html c++中,临时对象一旦不需要,就会调用析构函数,释放其占有的资源: ...