版本比较
题解:P12171 [蓝桥杯 2025 省 Python B] 最长字符串
正文
增加 12 行删除 12 行
34 行未改动3536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687886 行未改动959697989910010110210310434 行未改动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 润色。:::34 行未改动3536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687886 行未改动959697989910010110210310434 行未改动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 润色。:::