Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4903 | Accepted: 1316 |
Description
Input
Output
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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/zp___waj/article/details/47658889