版本比较
URAL - 1745 Yet Another Answer 题解
正文
增加 28 行删除 28 行
127 行未改动全部展开12812913013113213213313413513614 行未改动1511521531541551551561571581591601611621621631641651661671671681691701701711721731731741751761761771781791801811821831841841851861861871881891901911911921931941941951961971981992002012012022032042042052062072072082092102112112122132142142152162172182182192202212212222232242252262272272282292302312302312322332332342352362372382392392402412422432432442452462472482482492502512522532542552552562572572582592602612622632632642652662674 行未改动127 行未改动全部展开 } cout << ans.size() << "\n"; for (int i = (int)ans.size() - 1; i >= 0; i--) cout << a[ans[i]].id + 1 << ' '; cout << "\n";
return 0;}```### 一些实现细节14 行未改动 int balance; // 总平衡度(左括号数 - 右括号数) int min_prefix; // 最小前缀和 int id; // 单词原始编号 int length; // 单词长度
// 获取单词类型:正平衡返回1,负平衡返回-1,平衡返回0 int getType() const { if (balance > 0) return 1; if (balance < 0) return -1; return 0; }
// 排序比较函数 bool operator < (const Word &other) { int type1 = getType(); int type2 = other.getType();
// 不同类型按正>零>负排序 if (type1 != type2) return type1 > type2;
// 正平衡单词:按最小前缀和降序 if (type1 > 0) return min_prefix > other.min_prefix;
// 负平衡单词:按 (min_prefix - balance) 升序 if (type1 < 0) return (min_prefix - balance) < (other.min_prefix - other.balance);
return false; // 平衡单词顺序任意 }};
int main() { int word_count; cin >> word_count;
vector<Word> words(word_count);
// 读取并处理每个单词 for (int i = 0; i < word_count; i++) { string word_str; cin >> word_str;
int current_balance = 0; int current_min_prefix = 0;
// 计算单词的平衡度和最小前缀 for (char c : word_str) { if (c == '(') current_balance++; else current_balance--; current_min_prefix = min(current_min_prefix, current_balance); }
words[i] = {current_balance, current_min_prefix, i, (int)word_str.length()}; }
// 按特定规则排序单词 sort(words.begin(), words.end());
// 动态规划数组:dp[i][j] = 处理前i个单词,当前平衡度为j时的最大长度 vector<vector<int>> dp(word_count + 1, vector<int>(MAX_BALANCE + 1, INF)); vector<vector<int>> prev_index(word_count + 1, vector<int>(MAX_BALANCE + 1, -1));
// 初始状态:0个单词,平衡度为0,长度为0 dp[0][0] = 0;
// 动态规划处理每个单词 for (int i = 0; i < word_count; i++) { const Word ¤t_word = words[i];
for (int balance = 0; balance <= MAX_BALANCE; balance++) { if (dp[i][balance] == INF) continue;
// 选项1:不选当前单词 if (dp[i][balance] > dp[i + 1][balance]) { dp[i + 1][balance] = dp[i][balance]; prev_index[i + 1][balance] = prev_index[i][balance]; }
// 选项2:选当前单词(需要满足条件) int new_balance = balance + current_word.balance; if (balance + current_word.min_prefix >= 0 && new_balance >= 0 && new_balance <= MAX_BALANCE && if (balance + current_word.min_prefix >= 0 && new_balance >= 0 && new_balance <= MAX_BALANCE && dp[i][balance] + current_word.length > dp[i + 1][new_balance]) {
dp[i + 1][new_balance] = dp[i][balance] + current_word.length; prev_index[i + 1][new_balance] = i; } } }
// 输出结果 int max_length = dp[word_count][0]; cout << max_length << " ";
// 回溯找到使用的单词 vector<int> used_words; int current_index = word_count; int current_balance = 0;
while (prev_index[current_index][current_balance] != -1) { int word_idx = prev_index[current_index][current_balance]; used_words.push_back(word_idx); current_balance -= words[word_idx].balance; current_index = word_idx; }
cout << used_words.size() << "\n";
// 输出单词编号(注意反转顺序和+1处理) for (int i = (int)used_words.size() - 1; i >= 0; i--) { cout << words[used_words[i]].id + 1 << " "; } cout << "\n";
return 0;}```deepseek 把变量名改得更长了,但没有修改其他逻辑。实际写代码过程中,$b$,$d$ 这两个变量名还是记得住的。如果问题更复杂,就需要学习 deepseek 的命名习惯,防止大脑混乱。4 行未改动