码迷,mamicode.com
首页 > 其他好文 > 详细

找到所有变位词

时间:2016-01-23 07:57:35      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

找到所有变位词

标签:

原文地址:http://www.cnblogs.com/wuOverflow/p/5152749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!