版本比较
题解:P12171 [蓝桥杯 2025 省 Python B] 最长字符串
正文
增加 12 行删除 12 行
34 行未改动35363738393940414142434445464647484950505152535455565657585960616263636465666667686969707172737374757677777879808182838484858687886 行未改动95969798999910010110210310434 行未改动int main() { ifstream f("words.txt"); vector<string> w; string s;
while(getline(f, s)) w.push_back(s);
sort(w.begin(), w.end(), [](string& a, string& b) { if(a.size() != b.size()) return a.size() < b.size(); return a < b; });
unordered_map<string, string> dp; int mx = 0; string ans = "";
for(auto& t : w) { if(t.size() == 1) { string st = t; sort(st.begin(), st.end()); dp[st] = t;
if(t.size() > mx || (t.size() == mx && t < ans)) { mx = t.size(); ans = t; } } }
for(auto& t : w) { if(t.size() < 2) continue;
string ps = t.substr(0, t.size() - 1); sort(ps.begin(), ps.end());
if(dp.count(ps)) { string cs = t; sort(cs.begin(), cs.end());
if(!dp.count(cs) || t < dp[cs]) { dp[cs] = t; }
if(t.size() > mx || (t.size() == mx && t < ans)) { mx = t.size(); ans = t; } } }
cout << ans; return 0;}```6 行未改动
int main(){ cout<<"afplcu"; return 0;} }```
:::info[声明]{open}本文使用了 AI 润色。:::