#include<iostream> #include<map> #include<string> #include<set> using namespace std; int main() {     string s;     getline(cin, s);     map<char, int> count;     for (int i = 0;i < s.size();i++) {         if (count.find(s[i]) != count.end()) {             count[s[i]]++;         }         else             count.insert(pair<char, int>(s[i], 1));     }     map<int, set<string>> quanzhi;     for (map<char, int>::iterator it = count.begin();it != count.end();it++) {         string temp = "";         if (quanzhi.find((*it).second) != quanzhi.end()) {             quanzhi[(*it).second].insert(temp + (*it).first);         }         else {             set<string> temp1;             temp1.insert(temp + (*it).first);             quanzhi.insert(pair<int, set<string>>((*it).second, temp1 ));         }              }     map<char,string> ans;     while (quanzhi.size() > 1) {         string temp = "";         int a1, a2;         string s1, s2;         map<int, set<string>>::iterator it = quanzhi.begin();         a1 =(*it).first;         if ((*it).second.size() > 1) {             s1 = *(*it).second.begin();             (*it).second.erase((*it).second.begin());             a2= (*it).first;             s2= *(*it).second.begin();             (*it).second.erase((*it).second.begin());         }         else         {             s1 = *(*it).second.begin();             (*it).second.erase((*it).second.begin());             it++;             a2 = (*it).first;             s2 = *(*it).second.begin();             (*it).second.erase((*it).second.begin());         }         if (quanzhi[a1].size() == 0) {             if (a1 == a2) {                 quanzhi.erase(a1);             }             else             {                 quanzhi.erase(a1);                 if (quanzhi[a2].size() == 0)                     quanzhi.erase(a2);             }         }         if (a1 < a2) {             for (int i = 0;i < s1.size();i++) {                 if (ans.find(s1[i]) != ans.end()) {                     ans[s1[i]] = '0' + ans[s1[i]];                 }                 else                 {                     ans.insert(pair<char, string>(s1[i], "0"));                 }             }             for (int i = 0;i < s2.size();i++) {                 if (ans.find(s2[i]) != ans.end()) {                     ans[s2[i]] = '1' + ans[s2[i]];                 }                 else                 {                     ans.insert(pair<char, string>(s2[i], "1"));                 }             }         }         else if (a1 > a2) {             for (int i = 0;i < s1.size();i++) {                 if (ans.find(s1[i]) != ans.end()) {                     ans[s1[i]] = '1' + ans[s1[i]];                 }                 else                 {                     ans.insert(pair<char, string>(s1[i], "1"));                 }             }             for (int i = 0;i < s2.size();i++) {                 if (ans.find(s2[i]) != ans.end()) {                     ans[s2[i]] = '0' + ans[s2[i]];                 }                 else                 {                     ans.insert(pair<char, string>(s2[i], "0"));                 }             }         }         else         {             if (s1.size() <= s2.size()) {                 for (int i = 0;i < s1.size();i++) {                     if (ans.find(s1[i]) != ans.end()) {                         ans[s1[i]] = '0' + ans[s1[i]];                     }                     else                     {                         ans.insert(pair<char, string>(s1[i], "0"));                     }                 }                 for (int i = 0;i < s2.size();i++) {                     if (ans.find(s2[i]) != ans.end()) {                         ans[s2[i]] = '1' + ans[s2[i]];                     }                     else                     {                         ans.insert(pair<char, string>(s2[i], "1"));                     }                 }             }             else             {                 for (int i = 0;i < s1.size();i++) {                     if (ans.find(s1[i]) != ans.end()) {                         ans[s1[i]] = '1' + ans[s1[i]];                     }                     else                     {                         ans.insert(pair<char, string>(s1[i], "1"));                     }                 }                 for (int i = 0;i < s2.size();i++) {                     if (ans.find(s2[i]) != ans.end()) {                         ans[s2[i]] = '0' + ans[s2[i]];                     }                     else                     {                         ans.insert(pair<char, string>(s2[i], "0"));                     }                 }             }         }         if (quanzhi.find(a1 + a2) != quanzhi.end()) {             quanzhi[a1 + a2].insert(s1 + s2);         }         else {             set<string> tee;             tee.insert(s1 + s2);             quanzhi.insert(pair<int, set<string>>(a1 + a2, tee));         }     }     for (int i = 0;i < s.size();i++) {         cout << ans[s[i]];     } } 过了,但是代码挺垃圾的

相关推荐

沉淀一会:1.同学你面试评价不错,概率很大,请耐心等待; 2.你的排名比较靠前,不要担心,耐心等待; 3.问题不大,正在审批,不要着急签其他公司,等等我们! 4.预计9月中下旬,安心过节; 5.下周会有结果,请耐心等待下; 6.可能国庆节前后,一有结果我马上通知你; 7.预计10月中旬,再坚持一下; 8.正在走流程,就这两天了; 9.同学,结果我也不知道,你如果查到了也告诉我一声; 10.同学你出线不明朗,建议签其他公司保底! 11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
牛客网
牛客企业服务