POJ-2408 Anagram Groups-字典序取最大前五
2015-08-20 16:00
405 查看
Anagram Groups
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
Sample Output
Source
Ulm Local 2000
数据很多,用C语言输入输出流比较保险。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4906 | Accepted: 1319 |
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 <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef struct word { char s[100],ss[100];//存初始单词和按字典序排列过的单词 } Word; Word w[30005];//最多30000个单词 typedef struct Cou { int count;//相同单词的数目 char a[100],aa[100]; } Count; Count c[30005]; //下面是qsort排序要用的比较函数 int word_cmp(const void* x1,const void* y1)//整个原始单词表列排序用 { Word* x=(Word*)x1; Word* y=(Word*)y1; int m=strcmp(x->ss,y->ss); if(m!=0) return m; return strcmp(x->s,y->s); } int ccmp(const void* x1,const void* y1)//数目优先,单词次之以保证排序稳定 { Count* x=(Count*)x1; Count* y=(Count*)y1; int m=y->count-x->count; if(m!=0) return m; return strcmp(x->aa,y->aa); } int main() { int i=0,j,m,n=0,len,cnt=1; while(scanf("%s",w[i].s)!=EOF)//输入单词 { strcpy(w[i].ss,w[i].s);//先复制 sort(w[i].ss,w[i].ss+strlen(w[i].ss));//再按字典序排列 ++i; } len=i;//单词个数 qsort(w,len,sizeof(Word),word_cmp);//整个单词表列按字典序排列 char* str1=w[0].ss;//初始赋值 char* str2=w[0].s; for(i=1; i<=len; ++i) { m=strcmp(str1,w[i].ss);//判断字典序排列后单词的是否相同 if(m==0) cnt++;//相同则+1 else//不相同 { c .count=cnt;//记录下当前相同的个数 strcpy(c .a,str1);//将字符串复制下来方便输出 strcpy(c .aa,str2); str1=w[i].ss;//再次比较 str2=w[i].s; ++n;//不相同之后要重新开始比较 cnt=1; } } qsort(c,n,sizeof(Count),ccmp);//排序来找出前5个最大的变形词组 n=n>5?5:n;//只需要输出前5个最大的变形词组 for(i=0; i<n; i++)//输出 { printf("Group of size %d: ",c[i].count);//输出个数 str1=" ";//清空str1 for(j=0; j<len; j++)//遍历原始单词组 { m=strcmp(w[j].ss,c[i].a); if(m>0)//超出范围 break; if(m<0||strcmp(str1,w[j].s)==0)//不相同或者是重复出现就继续遍历 continue; str1=w[j].s;//给str1赋值方便下次比较 printf("%s ",w[j].s);//输出单词 } printf(".\n"); } return 0; }
数据很多,用C语言输入输出流比较保险。
相关文章推荐
- TI-BLE协议栈的GATT学习笔记-1
- shell公共函数functions
- iOS屏幕切换
- 梁祝(二胡独奏曲)
- 【LeetCode】219. Contains Duplicate II
- 关于ASP网页无法打开的解决方案
- 图标左右抖动动画
- JavaScript(二)-15-(JS自定义对象)
- Shell脚本中的自定义函数
- 【转】Tomcat配置文件入门
- iOS项目上传到AppStore步骤流程
- java 生成XML格式文件
- arcengine 线分割线
- 利用ADB Root权限破解锁屏密码(原)
- UIWebView内存泄漏解决办法(二)
- Understanding the SIP Via Header
- JavaScript(二)-14-(JS中的特有语句-forin)
- Ext.Net GridPanel的用法--间断更新中
- 商品规格选择页面
- 二维几何模板 - 圆和球有关计算模板