Code golf: find all anagrams

后端 未结 8 2133
再見小時候
再見小時候 2021-02-04 06:00

A word is an anagram if the letters in that word can be re-arranged to form a different word.

Task:

  • The shortest source code by character count to find

8条回答
  •  甜味超标
    2021-02-04 06:05

    C++, 542 chars

    #include 
    #include 
    #include 
    #include 
    #define ci const_iterator
    int main(){using namespace std;typedef string s;typedef vector vs;vs l;
    copy(istream_iterator(cin),istream_iterator(),back_inserter(l));map r;
    for (vs::ci i=l.begin(),e=l.end();i!=e;++i){s a=boost::to_lower_copy(*i);
    sort(a.begin(),a.end());r[a].push_back(*i);}for (map::ci i=r.begin(),e=r.end();
    i!=e;++i)if(i->second.size()>1)*copy(i->second.begin(),i->second.end(),
    ostream_iterator(cout," "))="\n";}
    

提交回复
热议问题