UVA 10157 - Expressions(dp卡特兰数高精度)

本文探讨了如何计算特定长度和深度的正确括号表达式的数量,并提供了一种使用动态规划的方法来解决这个问题。

Problem A: EXPRESSIONS 


Let X be the set of correctly built parenthesis expressions. The elements of X are strings consisting only of the characters �(� and �)�. The set X is defined as follows:

  • an empty string belongs to X
  • if A belongs to X, then (A) belongs to X
  • if both A and B belong to X, then the concatenation AB belongs to X.
For example, the following strings are correctly built parenthesis expressions (and therefore belong to the set X):

()(())()

(()(()))

The expressions below are not correctly built parenthesis expressions (and are thus not in X):

(()))(()

())(()

Let E be a correctly built parenthesis expression (therefore E is a string belonging to X).

The length of E is the number of single parenthesis (characters) in E.

The depth D(E) of E is defined as follows:

               ì 0 if E is empty 
D(E)= í D(A)+1 if E = (A), and A is in X 
               î max(D(A),D(B)) if E = AB, and A, B are in X

For example, the length of �()(())()� is 8, and its depth is 2.

What is the number of correctly built parenthesis expressions of length n and depth d, for given positive integers n and d?

Task 
Write a program which

  • reads two integers n and d
  • computes the number of correctly built parenthesis expressions of length n and depth d;
Input data 
Input consists of lines of pairs of two integers - n and d, at most one pair on line, £ n £ 300, 1 £ d£ 150. 
The number of lines in the input file is at most 20, the input may contain empty lines, which you don't need to consider.

Output data 
For every pair of integers in the input write single integer on one line - the number of correctly built parenthesis expressions of length n and depth d.

Example 
Input data                                   Output data 
6 2                 3 
300 150             1 

There are exactly three correctly built parenthesis expressions of length 6 and depth 2: 
(())() 
()(()) 
(()()) 

题意:求长度n,深度最多为d的括号种数。

思路:dp,dp[i][j]表示长度i,深度不超过j的种数,对于每种情况,取最左边的括号和右边每个位置进行匹配,剩下的为内部和外部两部分,种数为dp[k][j - 1]*dp[i - k - 1][j]。然后把所有加起来即可

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
const int MAXN = 155;

#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)

const int MAXSIZE = 1005;

struct bign {
	int s[MAXSIZE];
	bign ()	{memset(s, 0, sizeof(s));}
	bign (int number) {*this = number;}
	bign (const char* number) {*this = number;}
    
	void put();
	bign mul(int d);
	void del();
    
	bign operator =  (char *num);
	bign operator =  (int num);

	bool operator <  (const bign& b) const;
	bool operator >  (const bign& b) const { return b < *this; }
	bool operator <= (const bign& b) const { return !(b < *this); }
	bool operator >= (const bign& b) const { return !(*this < b); }
	bool operator != (const bign& b) const { return b < *this || *this < b;}
	bool operator == (const bign& b) const { return !(b != *this); }
    
	bign operator + (const bign& c);
	bign operator * (const bign& c);
	bign operator - (const bign& c);
	int  operator / (const bign& c);
	bign operator / (int k);
	bign operator % (const bign &c);
	int  operator % (int k);
	void operator ++ ();
	bool operator -- ();
};

bign bign::operator = (char *num) {
	s[0] = strlen(num);
	for (int i = 1; i <= s[0]; i++)
		s[i] = num[s[0] - i] - '0';
	return *this;
}

bign bign::operator = (int num) {
	char str[MAXSIZE];
	sprintf(str, "%d", num);
	return *this = str;
}

bool bign::operator < (const bign& b) const {
	if (s[0] != b.s[0])
		return s[0] < b.s[0];
	for (int i = s[0]; i; i--)
		if (s[i] != b.s[i])
			return s[i] < b.s[i];
	return false;
}

bign bign::operator + (const bign& c) {
	int sum = 0;
	bign ans;
	ans.s[0] = max(s[0], c.s[0]);

	for (int i = 1; i <= ans.s[0]; i++) {
		if (i <= s[0]) sum += s[i];
		if (i <= c.s[0]) sum += c.s[i];
		ans.s[i] = sum % 10;
		sum /= 10;
	}
	while (sum) {
		ans.s[++ans.s[0]] = sum % 10;
		sum /= 10;
	}
	return ans;
}

bign bign::operator * (const bign& c) {
	bign ans;
	ans.s[0] = 0; 

	for (int i = 1; i <= c.s[0]; i++){  
		int g = 0;  

		for (int j = 1; j <= s[0]; j++){  
			int x = s[j] * c.s[i] + g + ans.s[i + j - 1];  
			ans.s[i + j - 1] = x % 10;  
			g = x / 10;  
		}  
		int t = i + s[0] - 1;

		while (g){  
			++t;
			g += ans.s[t];
			ans.s[t] = g % 10;
			g = g / 10;  
		}  

		ans.s[0] = max(ans.s[0], t);
	}  
	ans.del();
	return ans;
}

bign bign::operator - (const bign& c) {
	bign ans = *this; int i;
	for (i = 1; i <= c.s[0]; i++) {
		if (ans.s[i] < c.s[i]) {
			ans.s[i] += 10;
			ans.s[i + 1]--;;
		}
		ans.s[i] -= c.s[i];
	}

	for (i = 1; i <= ans.s[0]; i++) {
		if (ans.s[i] < 0) {
			ans.s[i] += 10;
			ans.s[i + 1]--;
		}
	}

	ans.del();
	return ans;
}

int bign::operator / (const bign& c) {
	int ans = 0;
	bign d = *this;
	while (d >= c) {
		d = d - c;
		ans++;
	}
	return ans;
}

bign bign::operator / (int k) {
	bign ans; 
	ans.s[0] = s[0];
	int num = 0;  
	for (int i = s[0]; i; i--) {  
		num = num * 10 + s[i];  
		ans.s[i] = num / k;  
		num = num % k;  
	}  
	ans.del();
	return ans;
}

int bign:: operator % (int k){  
	int sum = 0;  
	for (int i = s[0]; i; i--){  
		sum = sum * 10 + s[i];  
		sum = sum % k;  
	}  
	return sum;  
} 

bign bign::operator % (const bign &c) {
	bign now = *this;
	while (now >= c) {
		now = now - c;
		now.del();
	}
	return now;
}

void bign::operator ++ () {
	s[1]++;
	for (int i = 1; s[i] == 10; i++) {
		s[i] = 0;
		s[i + 1]++;
		s[0] = max(s[0], i + 1);
	}
}

bool bign::operator -- () {
	del();
	if (s[0] == 1 && s[1] == 0) return false;

	int i;
	for (i = 1; s[i] == 0; i++)
		s[i] = 9;
	s[i]--;
	del();
	return true;
}

void bign::put() {
	if (s[0] == 0)
		printf("0");
	else
		for (int i = s[0]; i; i--)
			printf("%d", s[i]);
}

bign bign::mul(int d) {
	s[0] += d; int i;
	for (i = s[0]; i > d; i--)
		s[i] = s[i - d];
	for (i = d; i; i--)
		s[i] = 0;
	return *this;
}

void bign::del() {
	while (s[s[0]] == 0) {
		s[0]--;
		if (s[0] == 0) break;
	}
}

int n, d;
bign f[MAXN][MAXN];

void init() {
	int i, j, k;
	for (i = 0; i <= 150; i ++)
		f[0][i] = 1;
	for (i = 1; i <= 150; i ++) {
		for (j = 1; j <= 150; j ++) {
			for (k = 0; k < i; k ++) {
				f[i][j] = f[k][j - 1] * f[i - k - 1][j] + f[i][j];
			}
		}
	}
}

int main() {
	init();
	while (~scanf("%d%d", &n, &d)) {
		(f[n / 2][d] - f[n / 2][d - 1]).put();
		printf("\n");
	}
	return 0;
}


<think>好的,我现在需要帮助用户解决关于ESLint的no-unused-expressions规则的配置问题。用户提到可能涉及ESLint规则或其他代码检查工具的配置需求,所以首先要确认这个规则的具体情况。 首先,我回忆一下no-unused-expressions规则的作用。这个规则主要是用来检查代码中是否有未使用的表达式,也就是那些没有对程序状态产生任何影响的表达式。比如,用户可能写了类似`a + b;`这样的语句,虽然计算了a和b的和,但没有赋值给变量或用于其他操作,这样的表达式就是未使用的,可能会被此规则检测到并报错。 接下来,我需要考虑用户可能遇到的常见场景。例如,使用Chai.js这样的测试库时,可能会写出`expect(...).to.be.true;`这样的断言语句。这些语句在ESLint默认配置下可能被标记为未使用的表达式,导致错误。因此,用户可能需要调整规则配置来允许这种情况。 根据引用内容,用户之前提到过使用eslint-config-prettier来禁用与格式相关的规则,但这里的问题是关于未使用表达式的,属于逻辑层面的规则,所以需要直接配置ESLint的规则而不是通过Prettier处理。 用户可能已经在使用create-react-app,而引用中提到的方案三使用了react-app-rewired和customize-cra来覆盖配置。这时候,用户可能需要通过修改ESLint配置文件来调整规则设置,而不需要eject项目。 接下来,我需要整理配置这个规则的具体步骤。通常,在.eslintrc文件中,可以通过rules字段来设置。对于no-unused-expressions,需要允许某些表达式(如测试中的断言),可能需要设置allowTernary或者allowShortCircuit等选项。例如: { "rules": { "no-unused-expressions": ["error", { "allowShortCircuit": true, "allowTernary": true, "allowTaggedTemplates": false }] } } 另外,针对特定库如Chai.js,可能需要使用eslint-plugin-chai来解决,或者在规则中添加allow选项。例如使用插件后配置: { "plugins": ["chai-friendly"], "rules": { "no-unused-expressions": "off", "chai-friendly/no-unused-expressions": "error" } } 同时,用户可能需要知道如何在create-react-app项目中应用这些配置,尤其是如果项目是基于react-scripts的话。根据引用中的方案三,使用react-app-rewired来覆盖配置,可以修改config-overrides.js文件,将自定义的ESLint配置合并进去。 最后,要提醒用户注意版本兼容性,比如引用中提到react-app-rewired适用于4.x以下的版本,如果用户使用的是更高版本可能需要其他方法。此外,测试配置是否生效的方法,比如运行ESLint命令检查,或者查看IDE的错误提示是否消失。</think>### ESLint no-unused-expressions 规则配置解决方案 #### 规则定义与作用 `no-unused-expressions` 是 ESLint 核心规则之一,用于检测代码中未产生实际作用的表达式(例如未赋值或未参与逻辑判断的学运算)。该规则可避免因冗余代码导致的可读性问题和潜在错误。 #### 典型应用场景 1. 单元测试框架中的断言表达式(如 `expect(...).to.be.true`) 2. JavaScript 短路表达式(如 `a || b`) 3. 三元运算符表达式(如 `condition ? action1() : action2()`) #### 配置方法 通过 `.eslintrc` 配置文件进行设置,支持以下参: ```json { "rules": { "no-unused-expressions": ["error", { "allowShortCircuit": true, // 允许短路表达式 "allowTernary": true, // 允许三元运算符 "allowTaggedTemplates": false // 是否允许标记模板字符串 }] } } ``` #### 测试框架特殊配置 使用 Chai.js 等断言库时,建议安装专用插件: ```bash npm install eslint-plugin-chai-friendly --save-dev ``` 配置示例: ```json { "plugins": ["chai-friendly"], "rules": { "no-unused-expressions": "off", "chai-friendly/no-unused-expressions": "error" } } ``` #### 在 Create-React-App 中的实现 通过 `react-app-rewired` 覆盖默认配置(适用 react-scripts 4.x 以下版本): 1. 修改 `config-overrides.js`: ```javascript const { override, useEslintRc } = require('customize-cra'); module.exports = override(useEslintRc()); ``` 2. 创建 `.eslintrc` 文件写入自定义规则[^2][^3] #### 验证配置有效性 ```bash npx eslint yourfile.js # 或查看 IDE 的 ESLint 错误提示 ``` #### 注意事项 1. 新版 react-scripts (5.0+) 使用 `ESLINT_NO_DEV_ERRORS=true` 环境变量控制检查强度[^1] 2. 与 Prettier 配合时需确保 `eslint-config-prettier` 正确安装[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值