This commit is contained in:
HuangHai
2025-08-30 18:35:01 +08:00
commit 1f397eca87
6258 changed files with 1487241 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
int n;
int res;
void dfs(int u, string t) {
if (u == 2 * n + 1) {
if (t.size() == 0) res++;
return;
}
//当前填充{
dfs(u + 1, t + "{");
if (t.back() == '{') //如果t的最后一个字符是 {
t.pop_back(), dfs(u + 1, t); //当前填充},就会对掉一对{}
// t.substr(0,t.size()-1) 去掉尾巴
else
dfs(u + 1, t + "}");
}
int main() {
cin >> n;
dfs(1, "");
cout << res << endl;
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int res;
char stk[N];
bool check(string s) {
int tt = 0; // 因为是多次使用栈,所以这里tt需要每次清空为0
for (int i = s.size() - 1; i >= 0; i--) {
if (tt && stk[tt] == '}' && s[i] == '{')
tt--;
else
stk[++tt] = s[i];
}
return tt == 0;
}
void dfs(int u, string t) {
if (u == 2 * n + 1) {
if (check(t)) res++;
return;
}
dfs(u + 1, t + "{");
dfs(u + 1, t + "}");
}
int main() {
cin >> n;
dfs(1, "");
cout << res << endl;
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
int n;
int res;
stack<char> stk;
bool check(string t) {
stack<char>().swap(stk);
for (int i = t.size() - 1; i >= 0; i--) {
if (!stk.empty() && stk.top() == '}' && t[i] == '{')
stk.pop();
else
stk.push(t[i]);
}
return stk.size() == 0;
}
void dfs(int u, string t) {
if (u == 2 * n + 1) {
if (check(t)) res++;
return;
}
dfs(u + 1, t + "{");
dfs(u + 1, t + "}");
}
int main() {
cin >> n;
dfs(1, "");
cout << res << endl;
return 0;
}