记录编号 38132 评测结果 AAAATTT
题目名称 [IOI 1998][USACO 3.1] 联系 最终得分 57
用户昵称 GravatarMakazeu 是否通过 未通过
代码语言 C++ 运行时间 3.181 s
提交时间 2012-04-13 15:16:22 内存使用 0.69 MiB
显示代码纯文本
  1. /*
  2. ID:zyfwork1
  3. LANG:C++
  4. TASK:contact
  5. */
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <string>
  10. #include <iostream>
  11. #include <algorithm>
  12. #define loop(i,a,b) for(int i=a;i<=b;i++)
  13. using namespace std;
  14. const char base='0';
  15. const int sonnum=2;
  16. class Trie{public:bool isStr;class Trie *son[sonnum]; int pos;};
  17. class mystring{public:int times;char s[13];}S[100000];
  18. int A,B,N,M,top=0;
  19. char str[200010];
  20. Trie *Root;
  21. Trie *NewTrie()
  22. {
  23. Trie *temp=new Trie; loop(i,0,sonnum-1) temp->son[i]=NULL;
  24. temp->isStr=false,temp->pos=0; return temp;
  25. }
  26. void Find(Trie *pnt,int start,int len)
  27. {
  28. Trie *temp=pnt; loop(i,start,start+len-1)
  29. {
  30. if(temp->son[str[i]-base]==NULL)
  31. temp->son[str[i]-base]=NewTrie();
  32. temp=temp->son[str[i]-base];
  33. }
  34. if(!temp->isStr)
  35. {
  36. temp->isStr=true; M++; temp->pos=M;
  37. S[M].times=1; memset(S[M].s,'\0',sizeof(S[M].s));
  38. loop(i,start,start+len-1) S[M].s[i-start]=str[i]; return;
  39. }
  40. S[temp->pos].times++;
  41. }
  42.  
  43. inline bool cmp(const mystring& a,const mystring& b)
  44. {
  45. if(a.times!=b.times) return a.times>b.times;
  46. int al=strlen(a.s),bl=strlen(b.s);
  47. if(al!=bl) return al<bl;
  48. if(strcmp(a.s,b.s)>0) return 0;
  49. return 1;
  50. }
  51.  
  52. void init()
  53. {
  54. scanf("%d %d %d\n",&A,&B,&N);Root=NewTrie();
  55. char c; while(scanf("%c",&c)!=EOF) {if(c=='0' || c=='1') str[top++]=c;}
  56. for(int i=A;i<=B;i++)
  57. for(int len=0;len+i<=(int)strlen(str);len++)
  58. Find(Root,len,i);
  59. }
  60.  
  61. void solve()
  62. {
  63. sort(S+1,S+M+1,cmp);
  64. int p=1,n=0,now=-1994,pos=0;
  65. while(p<=M)
  66. {
  67. if(S[p].times!=now)
  68. {
  69. if(n==N) break;
  70. if(n!=0) printf("\n"); n++;
  71. pos=0; printf("%d\n%s",S[p].times,S[p].s);
  72. now=S[p].times; p++; continue;
  73. }
  74. pos++; if(!(pos%6)) printf("\n%s",S[p].s);
  75. else printf(" %s",S[p].s);
  76. p++;
  77. }
  78. printf("\n");
  79. }
  80.  
  81. int main()
  82. {
  83. freopen("contact.in","r",stdin);
  84. freopen("contact.out","w",stdout);
  85. init();
  86. solve();
  87. return 0;
  88. }