题意很简单,给出一堆单词,如果在单词a,b在某种排列时相等,则a、b相等。
然后只要将单词最小表示就可以。
贴代码
1 # include <cstdio>
2 # include <cstring>
3 # include <vector>
4 # include <string>
5 # include <algorithm>
6 using namespace std;
7 # include <map>
8 map<string,vector<string> > refer;
9 vector<pair<string,vector<string> > > ans;
10 void ssort(char *str)
11 {
12 int c[26],len=strlen(str);
13 memset(c,0,sizeof(c));
14 for(int i=0;i<len;i++)
15 c[str[i]-'a']++;
16 for(int i=0;i<26;i++)
17 while(c[i])
18 {
19 *str=i+'a';
20 c[i]--;
21 str++;
22 }
23 }
24 bool cmp(const pair<string,vector<string> > &a,const pair<string,vector<string> > &b)
25 {
26 if(a.second.size()!=b.second.size())
27 return a.second.size()>b.second.size();
28 else
29 return a.first<b.first;
30 }
31 int main()
32 {
33 // freopen("input.txt","r",stdin);
34 // freopen("output.txt","w",stdout);
35 char str[20];
36 while(scanf("%s",str)!=EOF)
37 {
38 string ori(str);
39 ssort(str);
40 refer[string(str)].push_back(ori);
41 }
42 for(map<string,vector<string> >::iterator it=refer.begin();it!=refer.end();it++)
43 ans.push_back(*it);
44 for(int i=0;i<ans.size();i++)
45 {
46 sort(ans[i].second.begin(),ans[i].second.end());
47 ans[i].first=ans[i].second[0];
48 }
49 sort(ans.begin(),ans.end(),cmp);
50 for(int i=0;i<5&&i<ans.size();i++)
51 {
52 vector<string>::iterator end=unique(ans[i].second.begin(),ans[i].second.end());
53 printf("Group of size %d: ",ans[i].second.size());
54 for(vector<string>::iterator it=ans[i].second.begin();it!=end;it++)
55 printf("%s ",it->c_str());
56 printf(".\n");
57 }
58 //system("pause");
59 return 0;
60 }
61