九度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 ...
随机推荐
- 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
贪婪的送礼者Greedy Gift Givers 难度:☆ Code: #include <iostream> #include <cstdio> #include <c ...
- Sass基本特性
Sass扩展/继承@extend 代码的继承,声明方式:.class;调用方式:@extend 如: .btn { border: 1px solid #ccc; padding: 6px 10px; ...
- ios has denied the launch request.
ios has denied the launch request. You can choose either of the two ways. Solution 1: Open System Pr ...
- JBOSS连接池默认连接数是多少?在哪个配置文件有这个默认的连接数?
如果你用的是是4.x的Jboss的话,请参考:docs/dtd/jboss-ds_1_0.dtd,相信你很容易就能找到控制最大/最小连接数的选项,应该是诸如:max-pool-size/min-poo ...
- https增加临时证书,tomcat配置
1Windows下: 1.1 生成keystore文件及导出证书 打开控制台: 运行: %JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RS ...
- SAP成都研究院郑晓霞:Shift Left Testing和软件质量保证的一些思考
今天的文章来自Jerry的同事,曾经的搭档郑晓霞(Zheng Kate).郑晓霞是在Jerry心中是一位很有实力的程序媛,2011年从西安某软件公司跳槽到SAP成都研究院.当时,成都研究院的CRM团队 ...
- CAD交互绘制批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment( pCustomEntity, pWorldDraw, curPt) { // 得到绘制参数. ...
- cmake 指定输出目录
$ mkdir ~/cpp-netlib-build $ cd ~/cpp-netlib-build $ cmake -DCMAKE_BUILD_TYPE=Debug \ > -DCMAKE_C ...
- 玩4K必备知识:HDMI1.4、2.0、2.0a、2.0b接口参数对比【扫盲贴】
https://www.4k123.com/thread-55369-1-1.html 前言:玩4K的同学都知道,HDMI接口是视频传输最常用的接口,但是这个接口却有好几个版本HDMI1.4.HDMI ...
- java后台验证码的生成
前台代码: <tr> <td>验证码</td> <td><input name="checkCode" type=" ...