九度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 ...
随机推荐
- Django的学习需要掌握的一些基础和初步搭建自己的框架
一.Django的学习需要掌握的一些基础 第一个需要注意的点:客户端发送过来的数据结构组成: 第二个需要注意的点:动态网页和静态网页 静态网页:用户发送请求,服务端找到对应的静态文件返回给浏览器,静态 ...
- angulajs中引用chart.js做报表,修改线条样式
目前还有个问题,在手机上看,当折线y轴值超过1000,会有点问题 1.下载chart js,可以用bower 命令下载 http://www.chartjs.org/docs/#line-chart- ...
- SQL语句,mysql数据库
sql语句,把一张表里的数据,和特定数据(固定常量)新插入另一张表 ,,, from wm_jobpoint INSERT INTO wm_department(departmentcode,depa ...
- 洛谷 P2947 [USACO09MAR]仰望Look Up
题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...
- SAP C/4HANA到底包含哪些产品?
2018年6月的SAPPHIRE(蓝宝石大会)上, SAP发布了新的商务软件套件:C/4HANA,意在通过SAP C/4HANA将前台应用和SAP Digital Core(数字化核心)S/4HANA ...
- 4个Linux服务器监控工具
下面是我想呈现给你的4个强大的监控工具. htop – 交互式进程查看器 你可能知道在机器上查看实时进程的标准工具top.如果不知道,请运行$ top看看,运行$ man top阅读帮助手册. hto ...
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...
- Mathematics-基础:散列函数
一,概念: 散列(HASH)函数H也称哈希函数.是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位.160位,比输入的串短),该串h被称为输入x的Hash值. ...
- WINDOWS-API:操作网络映射盘-WNetAddConnection2
首先在VC项目属性,开发依赖项里添加MPR.lib:然后,配置文件里填入以下信息. //本地映射盘符 MapDriver=T: //目标根目录 //MapSharedPath=\\192.168.0 ...