ZOJ 1960 Anagram Groups (string processing)

Source: Internet
Author: User
Tags printf

I'm using a map that first sorts the words by the number of letters they appear, and then sort the output.

Note that if a word appears more than two times, then the output group size is also counted, but the output word is the time to output the word only once.

#include <iostream> #include <cstdio> #include <memory.h> #include <string> #include <map
> #include <algorithm> #include <functional> #include <vector> using namespace std;
Char buf[500];
Map<map<int,int>,vector<string> >s;
Vector<vector<string> >ans; BOOL CMP (const Vector<string>&v1,const VECTOR&LT;STRING&GT;&AMP;V2) {return v1.size () >v2.size () | |
(V1.size () ==v2.size () &&v1<v2);
		} int main () {while (scanf ("%s", buf)!=eof) {map<int,int>cnt;
		int Len=strlen (BUF);
		for (int i=0;i<len;++i) {cnt[buf[i]]++;
	} s[cnt].push_back (String (BUF));
	} map<map<int,int>,vector<string> >::iterator It=s.begin ();
		for (; It!=s.end (); ++it) {sort (It->second.begin (), It->second.end ());
	Ans.push_back (It->second);
	} sort (Ans.begin (), Ans.end (), CMP);
	int acnt=5; for (int i=0;acnt&&i<ans.size (); ++i,--acnt) {printf ("Group of size%d:", Ans[i].size ());
		Ans[i].erase (Unique (Ans[i].begin (), Ans[i].end ()), Ans[i].end ());
		for (int j=0;j<ans[i].size (); ++j) {printf ("%s", Ans[i][j].c_str ());
	} printf (". \ n");
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.