1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
class Solution {
public:
map<string, int> solveEquations(string equations) {
vector<string> lines = split(equations, '\n');
map<string, vector<pair<string, int>>> deps; // var -> [(var, coeff), (const, value)]
map<string, int> constants; // var -> constant_sum
map<string, int> solution;
// Parse equations
for (string& line : lines) {
if (!parseEquation(line, deps, constants)) {
return {};
}
}
// Find variables that can be solved directly
queue<string> ready;
for (auto& [var, terms] : deps) {
bool hasOnlyConstants = true;
for (auto& [term, coeff] : terms) {
if (isVariable(term)) {
hasOnlyConstants = false;
break;
}
}
if (hasOnlyConstants) {
ready.push(var);
}
}
// Solve using topological sort approach
while (!ready.empty()) {
string var = ready.front();
ready.pop();
if (solution.count(var)) continue;
// Calculate value for this variable
int value = constants[var];
for (auto& [term, coeff] : deps[var]) {
if (isVariable(term)) {
if (!solution.count(term)) {
// Still depends on unsolved variable
continue;
}
value += solution[term] * coeff;
} else {
value += stoi(term) * coeff;
}
}
solution[var] = value;
// Update dependencies and check for newly solvable variables
for (auto& [otherVar, terms] : deps) {
if (solution.count(otherVar)) continue;
bool canSolve = true;
for (auto& [term, coeff] : terms) {
if (isVariable(term) && !solution.count(term)) {
canSolve = false;
break;
}
}
if (canSolve) {
ready.push(otherVar);
}
}
}
// Check if all variables are solved
for (auto& [var, terms] : deps) {
if (!solution.count(var)) {
return {}; // Unsolvable (circular dependency)
}
}
// Verify solution consistency
if (!verifySolution(lines, solution)) {
return {};
}
return solution;
}
private:
vector<string> split(string str, char delim) {
vector<string> result;
stringstream ss(str);
string item;
while (getline(ss, item, delim)) {
result.push_back(item);
}
return result;
}
bool isVariable(const string& term) {
return !term.empty() && isalpha(term[0]);
}
bool parseEquation(string line, map<string, vector<pair<string, int>>>& deps,
map<string, int>& constants) {
// Remove spaces
line.erase(remove(line.begin(), line.end(), ' '), line.end());
size_t eqPos = line.find('=');
if (eqPos == string::npos) return false;
string left = line.substr(0, eqPos);
string right = line.substr(eqPos + 1);
// Parse right side
vector<pair<string, int>> terms;
int constantSum = 0;
stringstream ss(right);
string token;
int sign = 1;
size_t pos = 0;
while (pos < right.length()) {
if (right[pos] == '+') {
sign = 1;
pos++;
} else if (right[pos] == '-') {
sign = -1;
pos++;
}
size_t nextOp = right.find_first_of("+-", pos);
if (nextOp == string::npos) nextOp = right.length();
string term = right.substr(pos, nextOp - pos);
if (isVariable(term)) {
terms.push_back({term, sign});
} else {
constantSum += sign * stoi(term);
}
pos = nextOp;
}
deps[left] = terms;
constants[left] = constantSum;
return true;
}
bool verifySolution(const vector<string>& lines, const map<string, int>& solution) {
for (const string& line : lines) {
string cleanLine = line;
cleanLine.erase(remove(cleanLine.begin(), cleanLine.end(), ' '), cleanLine.end());
size_t eqPos = cleanLine.find('=');
string left = cleanLine.substr(0, eqPos);
string right = cleanLine.substr(eqPos + 1);
int leftVal = solution.count(left) ? solution.at(left) : stoi(left);
int rightVal = evaluateExpression(right, solution);
if (leftVal != rightVal) return false;
}
return true;
}
int evaluateExpression(const string& expr, const map<string, int>& solution) {
int result = 0;
int sign = 1;
size_t pos = 0;
while (pos < expr.length()) {
if (expr[pos] == '+') {
sign = 1;
pos++;
} else if (expr[pos] == '-') {
sign = -1;
pos++;
}
size_t nextOp = expr.find_first_of("+-", pos);
if (nextOp == string::npos) nextOp = expr.length();
string term = expr.substr(pos, nextOp - pos);
if (isVariable(term)) {
result += sign * solution.at(term);
} else {
result += sign * stoi(term);
}
pos = nextOp;
}
return result;
}
};
|