找到所有变位词

typedef std::list<std::string>      List;
typedef std::map<std::string, List> Map;

Map getAnagrams(List& input)
{
    Map result;
    for (const auto& s : input){
        auto key = s;
        std::sort(key.begin(), key.end());
        auto loc = result.find (key);
        if (loc != result.end ()){
            loc->second.push_back (s);
        }else{
            result.insert ({key, List{s}});
        }
    }
    return result;
}

 

posted @ 2016-01-23 02:14  wu_overflow  阅读(160)  评论(0编辑  收藏  举报