Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

解题思路+AC代码

Posted by yousiki at 2016-07-29 08:34:53 on Problem 1690
对于一对括号是否应该删除,我的方法就是判断三个方面,如下:
1.这对括号内是否含有运算符,如果不含运算符,则必定可以删去。
2.这对括号的前面的第一个运算符是否是'-',如果不是,则必定可以删去。
(这里注意,如果在访问到第一个运算符之前访问到了'(',则应判定为可以删去,括号内容由这对没有删去的括号包含,且整体位于运算符之后,无论运算符是什么都不会影响)
3.这对括号的子一级是否含有运算符。如果是,则其是类似于这种形式:( ... +/- ... ),这是不能删去的;如果没有,则应是类似于下面这样: ( ( ( ... ) ) ),显然外层括号是多余的。可以删去。
强调,上述三个判断,任意一个符合就可以删去,无需全部满足要求。
代码如下:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<iostream>
using namespace std;
string in, out;
int T, i, j, k;
signed main(void) {
	for (scanf("%d", &T), getchar(); T; T--) {
		getline(cin, in), out = "";
		for (i = 0; i < (signed)in.length(); i++)if (in[i] == '(') {
			bool f1 = true, f2 = true, f3 = false;
			for (j = i + 1, k = 1; j < (signed)in.length() && k; j++) {
				if (in[j] == '(')k++;
				if (in[j] == ')')k--;
				if (in[j] == '-' || in[j] == '+')f1 = false, f3 |= k == 1;
			}
			for (k = i - 1; k >= 0; k--) {
				f2 = in[k] != '-';
				if (in[k] == '+' || in[k] == '-' || in[k] == '(')break;
			}
			if (f1 || f2 || (!f3))in[i] = in[--j] = ' ';
		}
		for (i = 0; i < (signed)in.length(); i++)if (in[i] != ' ')out += in[i];
		cout << out << endl;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator