UVA 10157 - Expressions(dp 카트라이더 고정밀)
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:
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
Input data
Input consists of lines of pairs of two integers - n and d, at most one pair on line,
2 £ 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("
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.