2018_1_30_Anagram Groups_等价类_用stl_乱搞

有问题问我
Anagram Groups
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5515 Accepted: 1580

Description

World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the largest anagram groups.

A text is a sequence of words. A word w is an anagram of a word v if and only if there is some permutation p of character positions that takes w to v. Then, w and v are in the same anagram group. The size of an anagram group is the number of words in that group. Find the 5 largest anagram groups.

Input

The input contains words composed of lowercase alphabetic characters, separated by whitespace(or new line). It is terminated by EOF. You can assume there will be no more than 30000 words.

Output

Output the 5 largest anagram groups. If there are less than 5 groups, output them all. Sort the groups by decreasing size. Break ties lexicographically by the lexicographical smallest element. For each group output, print its size and its member words. Sort the member words lexicographically and print equal words only once.

Sample Input

undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet

Sample Output

Group of size 5: caret carte cater crate trace .
Group of size 4: abet bate beat beta .
Group of size 4: ate eat eta tea .
Group of size 1: displayed .
Group of size 1: singleton .

Source

Ulm Local 2000
#include<iostream>
#include<algorithm>
#include<string> 
#include<cstring>
#include<map>
#include<set>
#include<iterator>
using namespace std;

typedef multiset<string> sstrset;
typedef map<string,sstrset> strsetmap;

typedef pair<int,string> intstr;
typedef map<intstr,string> intstrmap;

int main(){
	strsetmap group;
	string s;
	while(cin>>s){
		string sorted=s;
		sort(sorted.begin(),sorted.end());
		group[sorted].insert(s);
	}
	
	intstrmap gsize;
	for(strsetmap::iterator it=group.begin();it!=group.end();it++){
		intstr p(-it->second.size(),*it->second.begin());
		gsize[p]=it->first;
	}
	
	int count=5;
	for(intstrmap::iterator it=gsize.begin();it!=gsize.end();it++){
		if(count--==0)break;
		cout<<"Group of size "<<-it->first.first<<": ";
		
		unique_copy(group[it->second].begin(),group[it->second].end(),
		ostream_iterator<string>(cout," "));
		cout<<"."<<endl;
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值