Anagram Groups
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4903 | Accepted: 1316 |
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
对strcmp();认识不到位WA一天了 哎
AC:代码:
#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 strcmp(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\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\n"; ///for(int i=0;i<n;++i)me[i].put(); ///cout<<"\n\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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。