Anagram Groups
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4903 |
|
Accepted: 1316 |
Description
World-renowned Prof. A. N. Agram ' s current, deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in 中文版 language texts. Given such a text, you is 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 are some permutation p of character positions that takes W to v. Th En, W and V is in the same anagram group. The size of an anagram group are the number of words in. 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'll be is no more than 30000 words.
Output
Output the 5 largest anagram groups. If there is 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, the print its size and its member words. Sort the member words lexicographically and print equal words only once.
Sample Input
Undisplayedtraceteasingletonetaeatdisplayedcratecatercartecaretbetabeatbateateabet
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
to strcmp (); don't know. WA one day.
AC: Code:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace Std;struct my{Char w1[30]; Char w2[30]; int size; int Len; void put () {printf ("W1:%s W2:%s len:%d size:%d\n", w1,w2,len,size); }};struct on{int isize; int start; int end; void put () {printf ("Start:%d end:%d isize:%d\n", start,end,isize); }};on z[30010];my me[30010];bool cmp1 (my a,my b) {if (A.len==b.len) {if (strcmp (A.W2,B.W2) ==0) return s TRCMP (A.W1,B.W1) <0?true:false; else return strcmp (A.W2,B.W2) <0?true:false; } return A.len>b.len;} BOOL Cmp2 (on A,on b) {if (a.isize==b.isize) {return strcmp (ME[A.START].W1,ME[B.START].W1) <0?true:false; } return a.isize>b.isize;} int main () {char temp[30]; int n=0; while (scanf ("%s", temp) ==1) {int L=strlen (temp); strcpy (me[n].w1,temp); Me[n].len=l; me[n].size=1; Sort (temp,temp+l); Strcpy (me[n].w2,temp); n++; }///for (int i=0;i<n;++i) me[i].put (); Sort (ME,ME+N,CMP1); cout<< "\ n"; for (int i=0;i<n;++i) me[i].put (); int nz=0; for (int i=0,add,t;i<n;++i) {add=0;t=i; while (!STRCMP (ME[I].W2,ME[I+1].W2) &&i<n) {add++;i++; } for (int j=t;j<=i;j++) Me[j].size+=add; z[nz].start=t; Z[nz].end=i; z[nz].isize=add+1; nz++; } sort (Z,Z+NZ,CMP2); cout<< "\ n"; for (int i=0;i<n;++i) me[i].put (); cout<< "\ n"; for (int i=0;i<nz;++i) z[i].put (); int loop=5; int i=0; while (LOOP--&&I<N&&I<NZ) {printf ("Group of size%d:", z[i].isize); printf ("%s", ME[Z[I].START].W1); for (int j=z[i].start+1;j<=z[i].end;++j) {if (strcmp (ME[J].W1,ME[J-1].W1)!=0) printf ("%s", Me[j ].W1); } printf (". \ n"); i++; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2408 Anagram Groups sort to the extreme