diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..ec0373c
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,63 @@
+---
+Language: Cpp
+BasedOnStyle: WebKit
+AlignAfterOpenBracket: true
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignConsecutiveMacros: true
+AlignOperands: Align
+AlignTrailingComments: false
+AllowShortBlocksOnASingleLine: Empty
+AllowShortCaseLabelsOnASingleLine: true
+AllowShortIfStatementsOnASingleLine: Always
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakTemplateDeclarations: Yes
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: false
+ AfterControlStatement: false
+ AfterEnum: false
+ AfterFunction: false
+ AfterNamespace: false
+ AfterStruct: false
+ AfterUnion: false
+ AfterExternBlock: false
+ BeforeCatch: false
+ BeforeElse: false
+ BeforeLambdaBody: false
+ BeforeWhile: false
+ SplitEmptyFunction: false
+ SplitEmptyRecord: false
+ SplitEmptyNamespace: false
+BreakBeforeBraces: Custom
+BreakConstructorInitializers: AfterColon
+BreakConstructorInitializersBeforeComma: false
+ColumnLimit: 120
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+Cpp11BracedListStyle: true
+IncludeCategories:
+ - Regex: '^<.*'
+ Priority: 1
+ - Regex: '^".*'
+ Priority: 2
+ - Regex: '.*'
+ Priority: 3
+IncludeIsMainRegex: '([-_](test|unittest))?$'
+InsertNewlineAtEOF: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 2
+NamespaceIndentation: All
+PenaltyBreakAssignment: 0
+PenaltyBreakBeforeFirstCallParameter: 0
+ReferenceAlignment: Right
+#ReflowComments: false
+SortUsingDeclarations: Lexicographic
+SpaceInEmptyParentheses: false
+SpacesInAngles: Never
+SpacesInConditionalStatement: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+TabWidth: 4
+
diff --git a/.gitignore b/.gitignore
index 022166f..742acfa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,9 @@ cmake-build-debug-gcc
/*.dSYM
# 忽略 linux 编译产物
-/*
-!/*.*
-!*/
\ No newline at end of file
+**/*
+!**/*.*
+!*/
+
+*.dSYM/
+**/*.out
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e596ce6..c281a00 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,7 +7,7 @@
-
+
{
"useNewFormat": true
@@ -20,18 +20,24 @@
+
+
+
+
+
+
@@ -40,63 +46,101 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
+
+
@@ -152,97 +196,128 @@
- {
- "keyToString": {
- "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
- "C/C++ 文件.D_pojcs101_M02692.cpp.executor": "Run",
- "C/C++ 文件.luogu_P3916_graph_iteration.cpp.executor": "Debug",
- "C/C++ 文件.luogu_UVA755.cpp.executor": "Run",
- "C/C++ 文件.luogu_ez_B3628.cpp.executor": "Run",
- "C/C++ 文件.luogu_model_dsu_P3367.cpp.executor": "Run",
- "C/C++ 文件.noip2003j_bigint.cpp.executor": "Run",
- "C/C++ 文件.ojcs101_E01003.cpp.executor": "Run",
- "C/C++ 文件.ojcs101_E01218.cpp.executor": "Run",
- "C/C++ 文件.ojcs101_E02883.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M01017_6_boxes.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M01088_ski.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M01321_chest_grid.cpp.executor": "Debug",
- "C/C++ 文件.pojcs101_M01328_radar_greedy.cpp.executor": "Debug",
- "C/C++ 文件.pojcs101_M01852_ants_falling.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M01922_ride_follower.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M02039_snakey_encryption.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M02431_cows_gas_leak.cpp.executor": "Debug",
- "C/C++ 文件.pojcs101_M02524_ubiquitous_religions_dsu.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M02692.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M02692_12_coins.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M02694_polish_notation_tree.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M03247_palindromic_prime.cpp.executor": "Run",
- "C/C++ 文件.pojcs101_M03441_sum_zero.cpp.executor": "Run",
- "CMake 应用程序.JLOI2009_binary_tree.executor": "Run",
- "CMake 应用程序.NOIP2005J_herbs_dp_backpack.executor": "Run",
- "CMake 应用程序.NOIP2005J_herbs_dp_backpack_MWASTE_BETTER.executor": "Run",
- "CMake 应用程序.NOIP2006S_jinming_budget_dp_knapsack.executor": "Run",
- "CMake 应用程序.NOIP2016J_bay.executor": "Run",
- "CMake 应用程序.SHOI2002_ski_dp.executor": "Run",
- "CMake 应用程序.dp__TJOI2007_walk_segments_dp.executor": "Run",
- "CMake 应用程序.dsa__linear_list__dsa_0201_dictionary.executor": "Run",
- "CMake 应用程序.exercises.executor": "Run",
- "CMake 应用程序.gdbh.executor": "Run",
- "CMake 应用程序.graph__luogu_P3916_graph_iteration.executor": "Run",
- "CMake 应用程序.lc_135_candy.executor": "Run",
- "CMake 应用程序.luogu_P1160_biodir_linked_list.executor": "Run",
- "CMake 应用程序.luogu_P2196_miner_dp.executor": "Run",
- "CMake 应用程序.noip2003j_bigint.executor": "Run",
- "CMake 应用程序.pojcs101_M02694_polish_notation_tree.executor": "Run",
- "CMake 应用程序.pojcs101_M03441_sum_zero.executor": "Run",
- "CMake 应用程序.pojcs101_M03532.executor": "Run",
- "CMake 应用程序.pojcs101_M03704_brackets.executor": "Run",
- "CMake 应用程序.pojcs101_T01011_sticks.executor": "Run",
- "CMake 应用程序.pojcs101_T04117_split_int.executor": "Run",
- "CMake 应用程序.pojcs101_T12559_biggest_smallest_combination.executor": "Run",
- "CMake 应用程序.pojcs101_T27104_chicken_monitors.executor": "Run",
- "CMake 应用程序.pojcs101_T27373_biggest_combination_t2.executor": "Run",
- "CMake 应用程序.test.executor": "Run",
- "CMake 调试.exercises.executor": "Debug",
- "CidrDebugProfileConversion": "true",
- "ModuleVcsDetector.initialDetectionPerformed": "true",
- "Python.lc_127.executor": "Run",
- "Python.noi2001_food_chain_dsu.executor": "Run",
- "Python.pojcs101_M03247_palindromic_prime_init1.executor": "Debug",
- "RunOnceActivity.RadMigrateCodeStyle": "true",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "RunOnceActivity.cidr.known.project.marker": "true",
- "RunOnceActivity.git.unshallow": "true",
- "RunOnceActivity.readMode.enableVisualFormatting": "true",
- "RunOnceActivity.typescript.service.memoryLimit.init": "true",
- "RunOnceActivity.west.config.association.type.startup.service": "true",
- "cf.first.check.clang-format": "false",
- "cidr.known.project.marker": "true",
- "codeWithMe.voiceChat.enabledByDefault": "false",
- "deletionFromPopupRequiresConfirmation": "false",
- "git-widget-placeholder": "main",
- "last_opened_file_path": "/Users/roger/coding/exercises/graph",
- "node.js.detected.package.jshint": "true",
- "node.js.selected.package.jshint": "",
- "nodejs_package_manager_path": "npm",
- "org.rust.first.attach.projects": "true",
- "settings.editor.selected.configurable": "preferences.sourceCode.C++"
+
+}]]>
+
+
+
+
-
-
+
@@ -293,6 +368,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -308,6 +398,96 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -358,11 +538,46 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -478,6 +693,11 @@
+
+
+
+
+
@@ -538,7 +758,7 @@
-
+
@@ -554,7 +774,31 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -589,9 +833,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -602,9 +866,16 @@
+
+
+
+
+
+
+
@@ -644,84 +915,241 @@
-
+
+
+
+
-
+
+
- {
- "profileIdByStamp": [
+
+ "modificationCount": 133
+}]]>
@@ -757,13 +1185,14 @@
-
+
+
- file://$PROJECT_DIR$/noi2001_food_chain_dsu.py
+ file://$PROJECT_DIR$/NOI2001_food_chain_dsu.py
75
@@ -787,11 +1216,21 @@
66
+
+ file://$PROJECT_DIR$/dsa/linear_list/dsa_0202_card_stacking.cpp
+ 40
+
+
+
+ file://$PROJECT_DIR$/cs101/dh_20260917_01.cpp
+ 5
+
+
-
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 43fec66..588d036 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,9 +1,13 @@
cmake_minimum_required(VERSION 3.10)
-project(exercises)
+project(exercises C CXX)
+
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
-file(GLOB_RECURSE SOURCES "*.cpp")
+file(GLOB_RECURSE SOURCES "*.cpp" "*.c")
foreach (source ${SOURCES})
# 获取源文件所在目录(绝对路径)
@@ -22,7 +26,7 @@ foreach (source ${SOURCES})
endif ()
add_executable(${target_name} ${source})
- # 顺便把可执行文件输出到与源文件对应的目录中(可选)
+ # 把可执行文件输出到与源文件对应的目录中
set_target_properties(${target_name} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${rel_dir}"
)
diff --git a/noi2001_food_chain_dsu.py b/NOI2001_food_chain_dsu.py
similarity index 100%
rename from noi2001_food_chain_dsu.py
rename to NOI2001_food_chain_dsu.py
diff --git a/_others/jzh/jzh_c1_h.c b/_others/jzh/jzh_c1_h.c
new file mode 100644
index 0000000..1dfaa93
--- /dev/null
+++ b/_others/jzh/jzh_c1_h.c
@@ -0,0 +1,28 @@
+#include
+
+#define MAX_N 1001
+int n, s, max_s;
+int d[MAX_N];
+int sums[MAX_N];
+
+int main() {
+ scanf("%d%d", &n, &s);
+ for (int i = 1; i <= n; i++) {
+ scanf("%d", &d[i]);
+ }
+
+ sums[0] = s;
+ for (int i = 1; i <= n; i++) {
+ sums[i] = sums[i - 1] + d[i];
+ }
+
+ for (int i = 0; i <= n; i++) {
+ if (max_s < sums[i])
+ max_s = sums[i];
+ }
+
+ printf("%d\n", max_s);
+
+
+ return 0;
+}
diff --git a/cs101/dh_20260909_05_sum_fractions.cpp b/cs101/dh_20260909_05_sum_fractions.cpp
new file mode 100644
index 0000000..594a9a8
--- /dev/null
+++ b/cs101/dh_20260909_05_sum_fractions.cpp
@@ -0,0 +1,76 @@
+#include
+#include
+
+using namespace std;
+
+long long gcd(long long x, long long y) {
+ if (x < y) swap(x, y);
+ if (x % y == 0) { return y; }
+ return gcd(y, x % y);
+}
+
+class Fraction {
+public:
+ long long numerator = 0, denominator = 1;
+
+ void simplify() {
+ if (numerator == 0) { denominator = 1; }
+ long long g = gcd(numerator, denominator);
+ numerator /= g, denominator /= g;
+ }
+
+ Fraction operator+(const Fraction& other) const {
+ Fraction result{
+ numerator * other.denominator + denominator * other.numerator,
+ denominator * other.denominator
+ };
+ result.simplify();
+ return result;
+ }
+
+ Fraction operator+=(const Fraction& other) {
+ return *this = *this + other;
+ }
+
+ Fraction() = default;
+ Fraction(const long long n, const long long d) : numerator(n), denominator(d) { simplify(); }
+
+ Fraction(const string& s) {
+ stringstream ss(s);
+ long long n, d;
+ char slash;
+ if (ss >> n >> slash >> d && slash == '/') {
+ numerator = n, denominator = d;
+ simplify();
+ } else {
+ throw invalid_argument("String is not a fraction");
+ }
+ }
+
+ [[nodiscard]] string to_string() const {
+ if (denominator == 1) {
+ return std::to_string(numerator);
+ }
+ return std::to_string(numerator) + '/' + std::to_string(denominator);
+ }
+};
+
+int n;
+string s;
+Fraction ans;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ // clog << ans.numerator << '/' << ans.denominator << endl;
+ cin >> s;
+ ans += Fraction(s);
+ }
+
+ cout << ans.to_string() << endl;
+
+ return 0;
+}
diff --git a/cs101/dh_20260909_10_smaller_numbers.cpp b/cs101/dh_20260909_10_smaller_numbers.cpp
new file mode 100644
index 0000000..b644e10
--- /dev/null
+++ b/cs101/dh_20260909_10_smaller_numbers.cpp
@@ -0,0 +1,50 @@
+#include
+#include
+
+using namespace std;
+
+struct Number {
+ int value;
+ int index;
+
+ bool operator<(const Number& other) const {
+ return value < other.value;
+ }
+};
+
+constexpr int MAX_N = 1001;
+int n;
+Number numbers[MAX_N];
+int answers[MAX_N];
+
+void get_answers() {
+ sort(numbers, numbers + n);
+ int real_idx = 0;
+ for (int i = 0; i < n; i++) {
+ if (i == 0 || numbers[i].value != numbers[i - 1].value) { real_idx = i; }
+ answers[numbers[i].index] = real_idx;
+ }
+}
+
+void print_answers() {
+ for (int i = 0; i < n; i++) {
+ cout << answers[i] << endl;
+ }
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> n;
+ for (int i = 0; i < n; i++) {
+ cin >> numbers[i].value;
+ numbers[i].index = i;
+ }
+
+ get_answers();
+ print_answers();
+
+
+ return 0;
+}
diff --git a/cs101/dh_20260909_11_count_digits_init.py b/cs101/dh_20260909_11_count_digits_init.py
new file mode 100644
index 0000000..73dcb6d
--- /dev/null
+++ b/cs101/dh_20260909_11_count_digits_init.py
@@ -0,0 +1,17 @@
+def get_count_digits_init(n: int, d: int) -> int:
+ return str(n).count(str(d))
+
+
+MAX_N = 10001
+count_digits_init = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for _ in range(MAX_N)]
+
+if __name__ == '__main__':
+ for i in range(1, MAX_N):
+ for d in range(10):
+ count_digits_init[i][d] = get_count_digits_init(i, d) + count_digits_init[i - 1][d]
+ # with open('dh_20260909_11_count_digits_init.out', 'w') as f:
+ # f.write(str(count_digits_init).replace(" ", "").replace("]", "}").replace("[", "{"))
+
+ n, d = map(int, input().split())
+ print(count_digits_init[n][d])
+
diff --git a/cs101/dh_20260909_12_bottle_exchange.cpp b/cs101/dh_20260909_12_bottle_exchange.cpp
new file mode 100644
index 0000000..af0b960
--- /dev/null
+++ b/cs101/dh_20260909_12_bottle_exchange.cpp
@@ -0,0 +1,23 @@
+#include
+
+using namespace std;
+
+long long exchange_requirement, empty_bottles_amount, filled_bottles_amount, ans;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> exchange_requirement >> filled_bottles_amount;
+
+ while (true) {
+ ans += filled_bottles_amount;
+ empty_bottles_amount += filled_bottles_amount;
+ filled_bottles_amount = empty_bottles_amount / exchange_requirement;
+ empty_bottles_amount %= exchange_requirement;
+ if (filled_bottles_amount == 0) { break; }
+ }
+ cout << ans << endl;
+
+ return 0;
+}
\ No newline at end of file
diff --git a/cs101/dh_20260909_13_flip.cpp b/cs101/dh_20260909_13_flip.cpp
new file mode 100644
index 0000000..ddf773d
--- /dev/null
+++ b/cs101/dh_20260909_13_flip.cpp
@@ -0,0 +1,39 @@
+#include
+
+using namespace std;
+
+constexpr int MAX_N = 151;
+bool cards_flipped[MAX_N];
+
+void simulate() {
+ for (int divider=2; divider
+#include
+#include
+#include
+
+class Coord {
+public:
+ int x = 0, y = 0;
+ constexpr Coord(int x, int y) : x(x), y(y) {}
+
+ constexpr Coord operator+(const Coord other) const {
+ return {x + other.x, y + other.y};
+ }
+
+ constexpr Coord& operator+=(const Coord other) {
+ x += other.x, y += other.y;
+ return *this;
+ }
+
+ constexpr bool operator==(const Coord other) const {
+ return x == other.x && y == other.y;
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, const Coord coord) {
+ return os << "(" << coord.x << ", " << coord.y << ")";
+ }
+};
+
+constexpr int MAX_N = 10;
+int w, h, grid[MAX_N][MAX_N];
+std::string original_str;
+
+std::vector get_nums(const char c) {
+ if (c == ' ') { return {0, 0}; }
+ return {(c - 'A' + 1) / 10, (c - 'A' + 1) % 10};
+}
+
+Coord next_move(const Coord prev_coord, const Coord prev_facing) {
+ Coord new_coord = prev_coord + prev_facing;
+ if (new_coord.x < 0 || new_coord.x >= h ||
+ new_coord.y < 0 || new_coord.y >= w ||
+ grid[new_coord.x][new_coord.y] != -1) {
+ if (prev_facing == Coord{1, 0}) { return {0, -1}; }
+ if (prev_facing == Coord{0, -1}) { return {-1, 0}; }
+ if (prev_facing == Coord{-1, 0}) { return {0, 1}; }
+ if (prev_facing == Coord{0, 1}) { return {1, 0}; }
+ throw std::invalid_argument("Invalid facing direction");
+ }
+ return prev_facing;
+}
+
+std::ostream& print_grid(std::ostream& os) {
+ for (int i = 0; i < h; i++) {
+ for (int j = 0; j < w; j++) {
+ os << (grid[i][j] == -1 ? 0 : grid[i][j]);
+ }
+ }
+ os << std::endl;
+ return os;
+}
+
+void fill_grid(const std::string& s) {
+ memset(grid, -1, sizeof(grid));
+ Coord coord = {0, 0}, facing = {0, 1};
+ for (const char c : s) {
+ for (const int x : get_nums(c)) {
+ grid[coord.x][coord.y] = x;
+ std::clog << "Placing " << x << " at " << coord << std::endl;
+ print_grid(std::clog);
+ coord += facing = next_move(coord, facing);
+ }
+ }
+}
+
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ getline(std::cin, original_str);
+ std::stringstream ss(original_str);
+ ss >> h >> w;
+ getline(std::cin, original_str);
+ fill_grid(original_str);
+
+ print_grid(std::cout);
+
+
+ return 0;
+}
diff --git a/cs101/dh_20260909_15_jusephus.cpp b/cs101/dh_20260909_15_jusephus.cpp
new file mode 100644
index 0000000..39d3d8e
--- /dev/null
+++ b/cs101/dh_20260909_15_jusephus.cpp
@@ -0,0 +1,77 @@
+#include
+
+class CycledLinkedList {
+public:
+ struct Node {
+ int val = 0;
+ Node* next = nullptr;
+ Node* prev = nullptr;
+ };
+
+ Node* head = nullptr; // They are POINTERS, not OBJECTS.
+
+ void push_back(int val) {
+ if (head == nullptr) {
+ head = new Node({.val = val});
+ head->next = head;
+ head->prev = head;
+ return;
+ }
+ Node* node = new Node({.val = val});
+ head->prev->next = node;
+ node->prev = head->prev;
+ head->prev = node;
+ node->next = head;
+ }
+
+ void remove(Node* node) {
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+ if (node == head) {
+ head = node->next;
+ }
+ delete node;
+ }
+
+ void init_cycle(int n) {
+ for (int i = 0; i < n; i++) {
+ push_back(i);
+ }
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, const CycledLinkedList& list) {
+ for (Node* node = list.head->next; node != list.head; node = node->next) {
+ os << node->val << "<->";
+ }
+ os << list.head->val << std::endl;
+ return os;
+ }
+};
+
+int n, m, target_order;
+CycledLinkedList players;
+
+int target_idx() {
+ for (int i = 0; i < target_order - 1; i++) {
+ for (int j = 0; j < m - 1; j++) {
+ players.head = players.head->next;
+ }
+ players.remove(players.head);
+ }
+ for (int j = 0; j < m - 1; j++) {
+ players.head = players.head->next;
+ }
+ return players.head->val;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n >> m >> target_order;
+ players.init_cycle(n);
+ // std::clog << players << std::endl;
+ std::cout << target_idx() + 1 << std::endl;
+
+ return 0;
+}
diff --git a/cs101/dh_20260909_16_swap_bits.cpp b/cs101/dh_20260909_16_swap_bits.cpp
new file mode 100644
index 0000000..3b7da24
--- /dev/null
+++ b/cs101/dh_20260909_16_swap_bits.cpp
@@ -0,0 +1,86 @@
+#include
+#include
+
+constexpr size_t MAX_N = 201;
+int n, evaluation_value, original_evaluation_value;
+bool bits[MAX_N], original_bits[MAX_N], target_bits[MAX_N];
+
+std::ostream& print_bits(const bool* bits_list = bits, std::ostream& os = std::clog) {
+ for (int i = 0; i < n; i++) {
+ os << (bits_list[i] ? 1 : 0);
+ }
+ return os;
+}
+
+void swap_bits(int i, int j) {
+ if (bits[i] != target_bits[i]) { evaluation_value--; }
+ if (bits[j] != target_bits[j]) { evaluation_value--; }
+ std::swap(bits[i], bits[j]);
+ if (bits[i] != target_bits[i]) { evaluation_value++; }
+ if (bits[j] != target_bits[j]) { evaluation_value++; }
+}
+
+int evaluate() {
+ if (evaluation_value == 1) { return 1; }
+ return evaluation_value / 2;
+}
+
+bool dfs(int last_swap_pos = 0, int depth = 0, int target_depth = 0) {
+ const int h = evaluate();
+ if (h == 0) { return true; }
+ if (depth + h > target_depth) {
+ return false;
+ }
+ std::clog << "DEPTH=" << depth << "\th=" << h << "\t";
+ print_bits() << "\t";
+ print_bits(target_bits) << std::endl;
+ for (int i = 0; i < n - 1; i++) {
+ if (bits[i] == bits[i + 1]) { continue; }
+ if (i == last_swap_pos) { continue; }
+ if (bits[i] == target_bits[i] && bits[i + 1] == target_bits[i + 1]) { continue; }
+ if ((bits[i] == target_bits[i] || bits[i + 1] == target_bits[i + 1]) &&
+ (bits[i + 1] != target_bits[i] && bits[i] != target_bits[i + 1])) { continue; }
+ swap_bits(bits[i], bits[i + 1]);
+ if (dfs(i, depth + 1, target_depth)) { return true; }
+ swap_bits(bits[i], bits[i + 1]);
+ }
+ return false;
+}
+
+int ida_star() {
+ for (int target_depth = evaluate(); target_depth <= 2 * n; target_depth++) {
+ memcpy(bits, original_bits, n);
+ evaluation_value = original_evaluation_value;
+ std::clog << "target_depth:" << target_depth << std::endl;
+ if (dfs(-1, 0, target_depth)) {
+ return target_depth;
+ }
+ }
+ return -1;
+}
+
+bool init_evaluate() {
+ int original_sum = 0, target_sum = 0;
+ for (int i = 0; i < n; i++) {
+ original_sum += original_bits[i];
+ target_sum += target_bits[i];
+ original_evaluation_value += original_bits[i] != target_bits[i];
+ }
+ return original_sum == target_sum;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n;
+ for (int i = 0; i < n; i++) { std::cin >> original_bits[i]; }
+ for (int i = 0; i < n; i++) { std::cin >> target_bits[i]; }
+ if (!init_evaluate()) {
+ std::cout << -1 << std::endl;
+ return 0;
+ }
+ std::cout << ida_star() << std::endl;
+
+ return 0;
+}
diff --git a/cs101/dh_20260917_01.cpp b/cs101/dh_20260917_01.cpp
new file mode 100644
index 0000000..0b96253
--- /dev/null
+++ b/cs101/dh_20260917_01.cpp
@@ -0,0 +1,26 @@
+#include
+
+using namespace std;
+
+int afford_year(double salary, double price, int current_year = 1, double current_money = 0) {
+ if ((current_money += salary) >= price) { return current_year; }
+ if (current_year > 100) { return -1; }
+ return afford_year(salary * 1.08, price * 1.10, current_year + 1, current_money);
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ double money, price;
+ int ans;
+ cin >> money >> price;
+
+ if ((ans = afford_year(money, price)) == -1) {
+ cout << "Forget it." << endl;
+ } else {
+ cout << ans << endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_20260917_02_subway.cpp b/cs101/dh_20260917_02_subway.cpp
new file mode 100644
index 0000000..cef5cff
--- /dev/null
+++ b/cs101/dh_20260917_02_subway.cpp
@@ -0,0 +1,34 @@
+#include
+#include
+#include
+
+using namespace std;
+
+double get_price(int spent_money, int distance) {
+ double price = 0;
+ if (distance <= 6) {
+ price = 3;
+ } else {
+ price = 4;
+ }
+ distance -= 12;
+ if (distance > 0 && distance <= 32 - 12) { price += ceil(distance / 10.0); }
+ distance -= 32 - 12;
+ if (distance > 0) { price += ceil(distance / 20.0); }
+ if (spent_money >= 400) { return price; }
+ if (spent_money >= 150) { return price * 0.5; }
+ if (spent_money >= 100) { return price * 0.8; }
+ return price;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int spent_money, distance;
+ cin >> spent_money >> distance;
+ cout << fixed << setprecision(2) << get_price(spent_money, distance) << endl;
+
+
+ return 0;
+}
diff --git a/cs101/dh_20260917_03.cpp b/cs101/dh_20260917_03.cpp
new file mode 100644
index 0000000..e410155
--- /dev/null
+++ b/cs101/dh_20260917_03.cpp
@@ -0,0 +1,24 @@
+#include
+#include
+
+using namespace std;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ string token;
+ float coord[2];
+
+ for (int i = 0; getline(cin, token, ','); i++) {
+ coord[i] = stof(token);
+ }
+
+ if (-1 <= coord[0] && coord[0] <= 1 && -1 <= coord[1] && coord[1] <= 1) {
+ cout << "yes" << endl;
+ } else {
+ cout << "no" << endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_20260917_04.cpp b/cs101/dh_20260917_04.cpp
new file mode 100644
index 0000000..6457610
--- /dev/null
+++ b/cs101/dh_20260917_04.cpp
@@ -0,0 +1,37 @@
+#include
+#include
+
+using namespace std;
+
+vector calc_factors( int num) {
+ vector factors;
+ for (const int factor : {3,5,7}) {
+ if (num % factor == 0) {
+ factors.push_back(factor);
+ }
+ }
+ return factors;
+}
+
+int num;
+vector factors;
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> num;
+ factors = calc_factors(num);
+ if (factors.empty()) {
+ cout << "n" << endl;
+ } else {
+ int temp = factors[factors.size() - 1];
+ factors.pop_back();
+ for (const int factor : factors) {
+ cout << factor << " ";
+ }
+ cout << temp << endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_20260917_05_vote.cpp b/cs101/dh_20260917_05_vote.cpp
new file mode 100644
index 0000000..f8ac579
--- /dev/null
+++ b/cs101/dh_20260917_05_vote.cpp
@@ -0,0 +1,28 @@
+#include
+
+using namespace std;
+
+constexpr int MAX_X = 15;
+int n, winner;
+unsigned int bucket[MAX_X + 1];
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ int x;
+ cin >> x;
+ bucket[x]++;
+ }
+ winner = 0;
+ for (int x = 1; x <= MAX_X; x++) {
+ if (bucket[x] > bucket[winner]) {
+ winner = x;
+ }
+ }
+ cout << winner << endl;
+
+ return 0;
+}
diff --git a/cs101/dh_20260923_01_bike_or_walk.cpp b/cs101/dh_20260923_01_bike_or_walk.cpp
new file mode 100644
index 0000000..f0e214b
--- /dev/null
+++ b/cs101/dh_20260923_01_bike_or_walk.cpp
@@ -0,0 +1,29 @@
+#include
+
+double get_bike_time(double distance) {
+ return 27 + 23 + distance / 3;
+}
+
+double get_walk_time(double distance) {
+ return distance * 5 / 6;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int distance;
+ std::cin >> distance;
+ while (std::cin >> distance) {
+ double diff = get_bike_time(distance) - get_walk_time(distance);
+ if (diff < 1e-8 && diff > -1e-8) {
+ std::cout << "All" << std::endl;
+ } else if (diff < 0) {
+ std::cout << "Bike" << std::endl;
+ } else {
+ std::cout << "Walk" << std::endl;
+ }
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_20260923_02_draw_rectangles.cpp b/cs101/dh_20260923_02_draw_rectangles.cpp
new file mode 100644
index 0000000..769d3a6
--- /dev/null
+++ b/cs101/dh_20260923_02_draw_rectangles.cpp
@@ -0,0 +1,47 @@
+#include
+
+void print_filled_rectangle(int h, int w, char c) {
+ for (int i = 0; i < h; i++) {
+ for (int j = 0; j < w; j++) {
+ std::cout << c;
+ }
+ std::cout << std::endl;
+ }
+}
+
+void print_unfilled_rectangle(int h, int w, char c) {
+ for (int i = 0; i < w; i++) {
+ std::cout << c;
+ }
+ std::cout << std::endl;
+ for (int i = 0; i < h - 2; i++) {
+ std::cout << c;
+ for (int j = 0; j < w - 2; j++) {
+ std::cout << " ";
+ }
+ std::cout << c << std::endl;
+ }
+ for (int i = 0; i < w; i++) {
+ std::cout << c;
+ }
+ std::cout << std::endl;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int h, w;
+ char c;
+ bool filled;
+
+ while (std::cin >> h >> w >> c >> filled) {
+ if (filled) {
+ print_filled_rectangle(h, w, c);
+ } else {
+ print_unfilled_rectangle(h, w, c);
+ }
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_20260923_03.cpp b/cs101/dh_20260923_03.cpp
new file mode 100644
index 0000000..14cc889
--- /dev/null
+++ b/cs101/dh_20260923_03.cpp
@@ -0,0 +1,31 @@
+#include
+#include
+
+int get_digit(int num, int digit) {
+ num /= static_cast(std::pow(10, digit));
+ // std::clog << num << " is " << digit << "->" << std::endl;
+ return num % 10;
+}
+
+bool is_good_num(int num) {
+ return get_digit(num, 0)
+ - (get_digit(num, 3) + get_digit(num, 2) + get_digit(num, 1))
+ > 0;
+}
+
+int n, ans;
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ int num;
+ std::cin >> num;
+ ans += is_good_num(num);
+ }
+ std::cout << ans << std::endl;
+
+ return 0;
+}
diff --git a/cs101/dh_20260923_04_lamps.cpp b/cs101/dh_20260923_04_lamps.cpp
new file mode 100644
index 0000000..c97d76b
--- /dev/null
+++ b/cs101/dh_20260923_04_lamps.cpp
@@ -0,0 +1,30 @@
+#include
+
+constexpr int MAX_N = 1001;
+
+int n;
+bool lamps[MAX_N]; // 第 0 盏放弃
+
+void simulate() {
+ for (int i = 1; i <= n; i++) {
+ for (int j = 0; j <= n; j += i) {
+ lamps[j] = !lamps[j];
+ }
+ }
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cin >> n;
+ simulate();
+
+ for (int i = 1; i <= n; i++) {
+ if (!lamps[i]) {
+ std::cout << i << " ";
+ }
+ }
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/dsa/linear_list/dsa_0202_card_stacking.cpp b/dsa/linear_list/dsa_0202_card_stacking.cpp
new file mode 100644
index 0000000..7ece613
--- /dev/null
+++ b/dsa/linear_list/dsa_0202_card_stacking.cpp
@@ -0,0 +1,71 @@
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+class CycledLinkedList {
+public:
+ struct Node {
+ int prev_id, next_id;
+ };
+
+ vector nodes;
+
+ explicit CycledLinkedList(int n = 1) { nodes.reserve(n); }
+
+ void init_cycle(int n) {
+ assert(nodes.empty());
+ nodes.reserve(n);
+ for (int i = 0; i < n; i++) {
+ nodes.push_back({(i + n - 1) % n, (i + 1) % n});
+ }
+ }
+
+ void remove_node(int id) {
+ assert(id >= 0 && id < nodes.size());
+ if (nodes[id].prev_id != -1) { nodes[nodes[id].prev_id].next_id = nodes[id].next_id; }
+ if (nodes[id].next_id != -1) { nodes[nodes[id].next_id].prev_id = nodes[id].prev_id; }
+ nodes[id].prev_id = nodes[id].next_id = -1;
+ }
+};
+
+int cow_amount, good_cards_amount, cards_amount, pass_amount;
+CycledLinkedList cards;
+
+vector simulate() {
+ vector good_nodes;
+ int current_node = 0, previous_node = -1, cow = 1;
+ while (good_nodes.size() < good_cards_amount) {
+ // clog << "current_node: " << current_node << endl;
+ if (cow == 0) { good_nodes.push_back(current_node); }
+ previous_node = current_node, current_node = cards.nodes[current_node].next_id;
+ cards.remove_node(previous_node);
+ if (previous_node == current_node) { break; }
+ cow = (cow + 1) % cow_amount;
+ for (int _ = 0; _ < pass_amount; _++) {
+ current_node = cards.nodes[current_node].next_id;
+ }
+ }
+ return good_nodes;
+}
+
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ cin >> cow_amount >> cards_amount >> pass_amount;
+ good_cards_amount = cards_amount / cow_amount;
+
+ cards.init_cycle(cards_amount);
+
+ vector good_nodes = simulate();
+ sort(good_nodes.begin(), good_nodes.end());
+ for (const int node : good_nodes) {
+ cout << node + 1 << endl;
+ }
+
+ return 0;
+}
diff --git a/dsa/linear_list/dsa_0203_string_insertion.cpp b/dsa/linear_list/dsa_0203_string_insertion.cpp
new file mode 100644
index 0000000..2371165
--- /dev/null
+++ b/dsa/linear_list/dsa_0203_string_insertion.cpp
@@ -0,0 +1,29 @@
+#include
+
+using namespace std;
+
+string inserted_string(string s, const string& sub_s) {
+ int max_char_pos = -1;
+ char max_char = 0;
+ for (int i = 0; i < s.size(); i++) {
+ if (s[i] > max_char) {
+ max_char = s[i];
+ max_char_pos = i;
+ }
+ }
+ s.insert(max_char_pos + 1, sub_s); // STL 偷懒
+ return s;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ string s, sub_s;
+
+ while (cin >> s >> sub_s) {
+ cout << inserted_string(s, sub_s) << endl;
+ }
+
+ return 0;
+}
diff --git a/dsa/linear_list/dsa_0204_prefix_cycle.cpp b/dsa/linear_list/dsa_0204_prefix_cycle.cpp
new file mode 100644
index 0000000..f7f1fba
--- /dev/null
+++ b/dsa/linear_list/dsa_0204_prefix_cycle.cpp
@@ -0,0 +1,41 @@
+#include
+#include
+
+using namespace std;
+
+constexpr int MAX_N = 1e6 + 5;
+int max_period[MAX_N];
+
+void log_array(const int arr[], const size_t n) {
+ for (size_t i = 0; i < n; i++) {
+ clog << arr[i] << " ";
+ }
+ clog << endl;
+}
+
+void solve(string s) {
+ memset(max_period, 0, sizeof(max_period));
+ max_period[0] = 1;
+ for (int i = 1; i < s.size(); i++) {
+ if (s[i - max_period[i - 1]] == s[i]) {
+ max_period[i] = max_period[i - 1];
+ } else {
+ max_period[i] = i + 1;
+ }
+ }
+ log_array(max_period, s.size());
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(nullptr);
+
+ int n;
+ string s;
+ while (cin >> n && n) {
+ cin >> s;
+ solve(s);
+ }
+
+ return 0;
+}
diff --git a/dsa/linear_list/dsa_0205_polynomial_addition.cpp b/dsa/linear_list/dsa_0205_polynomial_addition.cpp
new file mode 100644
index 0000000..8d3e3b3
--- /dev/null
+++ b/dsa/linear_list/dsa_0205_polynomial_addition.cpp
@@ -0,0 +1,49 @@
+#include
+#include
+#include