http://www.leetcode.com/onlinejudge
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
multimap<string,string> map;
for(int i=0;i<strs.size();i++) {
if(strs[i].length()>0){
string value = strs[i];
sort(strs[i].begin(),strs[i].end()); //string也有begin(),end()方法
map.insert(pair<string,string>(strs[i], value));
}
}
vector<string> result;
if(map.empty()) {
return result;
}
multimap<string,string>::iterator it=map.begin();
string lastkey = it->first;
string lastvalue=it->second;
for(it++;it!=map.end();) {
if(it->first == lastkey) {
result.push_back(lastvalue);
while(it!=map.end() && it->first == lastkey)
{
result.push_back(it->second);
it++;
}
}
else {
lastkey = it->first;
lastvalue=it->second;
it++;
}
}
return result;
}
};