Skip to content

Commit f7eded7

Browse files
committed
add advance 1176-1179
1 parent 490fabf commit f7eded7

5 files changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fib = [0, 1]
2+
3+
count = 0
4+
while fib[-1] <= 10**8:
5+
new = fib[-1] + fib[-2]
6+
fib.append(new)
7+
8+
v = int(input())
9+
ans, diff = -1, 10**8
10+
for i in range(len(fib)):
11+
if fib[i] >= v:
12+
if abs(fib[i] - v) < diff:
13+
ans = fib[i]
14+
break
15+
else:
16+
ans = fib[i]
17+
diff = abs(fib[i] - v)
18+
print(ans)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
int main() {
6+
ios::sync_with_stdio(false);
7+
string s, p;
8+
cin >> s >> p;
9+
10+
pair<int, string> ans{10e5+10, ""};
11+
// brute force match p in s and find the shortest substring
12+
for (int i = 0; i < s.size(); i++) {
13+
if (s[i] != p[0]) continue;
14+
int l = i, r = l+1;
15+
int k = 1;
16+
for (; r < s.size() && k != p.size(); r++) {
17+
if (s[r] == p[k]) k++;
18+
if (k == p.size()) break;
19+
}
20+
21+
int len = r-l+1;
22+
if (k == p.size() && len < ans.first) {
23+
ans.first = len;
24+
ans.second = s.substr(l, len);
25+
}
26+
}
27+
28+
cout << ans.second << endl;
29+
return 0;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <stack>
5+
#include <unordered_map>
6+
#include <sstream>
7+
8+
using namespace std;
9+
10+
unordered_map<string, string> BuildChildParentMap(int n) {
11+
unordered_map<string, string> child_parent_map;
12+
stack<string> s;
13+
string line, id;
14+
15+
for (int i = 0; i < n; ++i) {
16+
getline(cin, line);
17+
int depth = line.find_first_not_of(' ');
18+
id = line.substr(depth);
19+
while (s.size() > depth) s.pop();
20+
if (!s.empty()) child_parent_map[id] = s.top();
21+
s.push(id);
22+
}
23+
return child_parent_map;
24+
}
25+
26+
// Function to trace back the path from a node to the root
27+
string GetPath(const string& id, const unordered_map<string, string>& child_parent_map) {
28+
vector<string> path;
29+
string cur = id;
30+
31+
while (child_parent_map.find(cur) != child_parent_map.end()) {
32+
path.push_back(cur);
33+
cur = child_parent_map.at(cur);
34+
}
35+
path.push_back("0000"); // Root ID
36+
37+
string path_str;
38+
for (auto it = path.rbegin(); it != path.rend(); ++it) {
39+
if (it != path.rbegin()) path_str += "->";
40+
path_str += *it;
41+
}
42+
43+
return path_str;
44+
}
45+
46+
int main() {
47+
ios::sync_with_stdio(false);
48+
int n;
49+
cin >> n;
50+
cin.ignore(); // Ignore the newline after reading n
51+
52+
auto child_parent_map = BuildChildParentMap(n);
53+
54+
string queries_line;
55+
getline(cin, queries_line);
56+
istringstream iss(queries_line);
57+
string query;
58+
iss >> query; // Skip the first part, which indicates the number of queries
59+
60+
while (iss >> query) {
61+
if (child_parent_map.find(query) != child_parent_map.end() || query == "0000") cout << GetPath(query, child_parent_map) << endl;
62+
else cout << "Error: " << query << " is not found." << endl;
63+
}
64+
65+
return 0;
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Dictionary to store child -> parent relationships
2+
parent_dict = {}
3+
4+
5+
def build_tree(lines):
6+
# Stack to keep track of the current path
7+
stack = []
8+
# Parse the tree structure
9+
for line in lines:
10+
# Count the leading spaces to determine the depth
11+
depth = len(line) - len(line.lstrip(" "))
12+
# Get the ID without leading spaces
13+
id = line.strip()
14+
# If the stack is longer than the depth, pop elements
15+
while len(stack) > depth:
16+
stack.pop()
17+
# If the stack is not empty, the last element is the parent
18+
if stack:
19+
parent_dict[id] = stack[-1]
20+
# Push the current id onto the stack
21+
stack.append(id)
22+
23+
24+
# Function to trace back the path from a node to the root
25+
def get_path(id):
26+
path = []
27+
while id in parent_dict:
28+
path.append(id)
29+
id = parent_dict[id]
30+
path.append("0000") # Root ID
31+
return "->".join(reversed(path))
32+
33+
34+
n = int(input())
35+
lines = []
36+
for i in range(n):
37+
lines.append(input())
38+
build_tree(lines)
39+
40+
queries = input().split()[1:]
41+
# Process the queries
42+
for query in queries:
43+
# Check if the ID exists in the tree
44+
if query in parent_dict or query == "0000":
45+
print(get_path(query))
46+
else:
47+
print(f"Error: {query} is not found.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <sstream>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <unordered_map>
7+
#include <unordered_set>
8+
using namespace std;
9+
10+
void parse_input(
11+
vector<int> &reactants,
12+
vector<int> &products,
13+
unordered_map<int, vector<vector<int>>> &reactions) {
14+
15+
int n, m, k;
16+
cin >> n;
17+
reactants.resize(n);
18+
for (int i = 0; i < n; ++i) cin >> reactants[i];
19+
20+
cin >> m;
21+
products.resize(m);
22+
// store products in reverse order, to reduce the time of vector::pop_back when searching
23+
for (int i = 0; i < m; ++i) cin >> products[m-i-1];
24+
25+
cin >> k;
26+
string line;
27+
getline(cin, line); // ignore the newline character after k
28+
unordered_set<int> ignore_self_products;
29+
for (int i = 0; i < k; ++i) {
30+
int reactions_product;
31+
vector<int> reactions_reactants;
32+
33+
string part;
34+
bool next_part_is_product = false;
35+
getline(cin, line);
36+
stringstream ss(line);
37+
// split the line by space, get reactants and product, store in reactions
38+
while (getline(ss, part, ' ')) {
39+
if (part == "+") continue;
40+
else if (part == "->") { next_part_is_product = true; continue; }
41+
42+
if (next_part_is_product) reactions_product = stoi(part);
43+
else reactions_reactants.push_back(stoi(part));
44+
}
45+
46+
// check if the product is the same as the reactants
47+
reactions[reactions_product].push_back(reactions_reactants);
48+
if (reactions_reactants.size() == 1 && *reactions_reactants.begin() == reactions_product)
49+
ignore_self_products.insert(reactions_product);
50+
}
51+
52+
// fill in self reactions if not exist
53+
for (int i = 0; i < m; i++) {
54+
int product = products[i];
55+
if (ignore_self_products.find(product) != ignore_self_products.end()) continue;
56+
reactions[product].push_back(vector<int>{ product });
57+
}
58+
}
59+
60+
// backtracking search
61+
void search(
62+
vector<int> &remain_products,
63+
unordered_set<int> &remain_reactants,
64+
unordered_map<int, vector<vector<int>>> &reactions,
65+
vector<pair<int, vector<int>>> &solution,
66+
bool &is_found) {
67+
68+
// if found solution, stop searching
69+
if (is_found) return;
70+
71+
// if no more products remain, got a solution, set is_found flag
72+
if (remain_products.empty()) {
73+
is_found = true;
74+
return;
75+
}
76+
77+
int product = remain_products.back();
78+
for (auto &reactant_set : reactions[product]) {
79+
// validation check if the reactants are all in remain_reactants
80+
bool is_valid_reaction = true;
81+
for (auto &reactant : reactant_set) {
82+
if (remain_reactants.find(reactant) == remain_reactants.end()) {
83+
is_valid_reaction = false;
84+
break;
85+
}
86+
}
87+
if (!is_valid_reaction) continue;
88+
89+
// record context
90+
for (auto &reactant : reactant_set) remain_reactants.erase(reactant);
91+
remain_products.pop_back();
92+
solution.push_back({product, reactant_set});
93+
94+
// search next
95+
search(remain_products, remain_reactants, reactions, solution, is_found);
96+
if (is_found) return; // check point for early stop if found solution
97+
98+
// back
99+
for (auto &reactant : reactant_set) remain_reactants.insert(reactant);
100+
remain_products.push_back(product);
101+
solution.pop_back();
102+
}
103+
}
104+
105+
int main() {
106+
ios::sync_with_stdio(false);
107+
108+
vector<int> reactants;
109+
vector<int> products;
110+
unordered_map<int, vector<vector<int>>> reactions;
111+
112+
parse_input(reactants, products, reactions);
113+
114+
// sort reactions by reactants order to find the 1st solution as best solution
115+
for (auto &reaction : reactions) {
116+
sort(reaction.second.begin(), reaction.second.end(), [&](const vector<int> &a, const vector<int> &b) {
117+
for (int j = 0; j < a.size() && j < b.size(); j++) {
118+
if (a[j] != b[j]) return a[j] < b[j];
119+
}
120+
});
121+
}
122+
123+
auto r = unordered_set<int>(reactants.begin(), reactants.end());
124+
vector<pair<int, vector<int>>> solution;
125+
bool is_find = false;
126+
search(products, r, reactions, solution, is_find);
127+
128+
for (auto &s : solution) {
129+
cout << setfill('0') << setw(2) << s.second[0];
130+
for (int i = 1; i < s.second.size(); i++) cout << " + " << setfill('0') << setw(2) << s.second[i];
131+
cout << " -> " << setfill('0') << setw(2) << s.first << endl;
132+
}
133+
134+
return 0;
135+
}

0 commit comments

Comments
 (0)