Guest User

Untitled

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <fstream>
  2. #include <cmath>
  3. using namespace std;
  4. int String2Int(char *str, int len)
  5. {
  6. int temp = 0;
  7. int s = 1;
  8. for (int i = len-1; i>=0 ;i--)
  9. {
  10. temp += (str[i]-'0')*s;
  11. s *= 2;
  12. }
  13. return temp;
  14. }
  15. char res[13];
  16. char* Int2String(int num,int len)
  17. {
  18. res[len] = '\0';
  19. for (int i = len-1 ; i>=0 ;i--)
  20. {
  21. if (num)
  22. {
  23. res[i] = '0' + num%2;
  24. num = num/2;
  25. }
  26. else
  27. {
  28. res[i] = '0';
  29. }
  30. }
  31. return res;
  32. }
  33.  
  34. int hash[13][10000];//len,binary
  35. char input[200001];
  36. class item
  37. {
  38. public:
  39. int freq,len,num;
  40. };
  41. item topn[100000];//
  42.  
  43. int cmp(const void* a, const void* b)
  44. {
  45. if (((item*)a)->freq ==((item*)b)->freq)
  46. {
  47. if (((item*)a)->len ==((item*)b)->len)
  48. {
  49. return ((item*)a)->num - ((item*)b)->num;
  50. }
  51. else
  52. {
  53. return ((item*)a)->len - ((item*)b)->len;
  54. }
  55. }
  56. else
  57. {
  58. return ((item*)b)->freq - ((item*)a)->freq;
  59. }
  60.  
  61. }
  62.  
  63. int main()
  64. {
  65. ifstream fin("contact.in");
  66. ofstream fout("contact.out");
  67. int A,B,N;
  68. fin>>A>>B>>N;
  69. char c;
  70. int size = 0;
  71. while ( (c = fin.get())!= EOF )
  72. {
  73. if (c=='\n');
  74. else
  75. input[size++] = c;
  76. }
  77. //process
  78. char buf[13];
  79. for ( int i = 0; i<size; i++ )
  80. {
  81. for (int length = A; length<=B ;length++)
  82. {
  83. if (i+length<=size)
  84. {
  85. for (int j = i ; j<i+length ;j++)
  86. {
  87. buf[j-i] = input[j];
  88. }
  89. hash[length][String2Int(buf,length)]++;
  90. }
  91. }
  92. }
  93. //sort
  94. int size_top = 0;
  95. for (int i = A ; i<=B ;i++)
  96. {
  97. for (int j =0 ; j<pow(2.0,i+1) ; j++)//attention here is i+1 instead of i.
  98. {
  99. topn[size_top].freq = hash[i][j];
  100. topn[size_top].len = i;
  101. topn[size_top].num = j;
  102. size_top++;
  103. }
  104. }
  105. qsort(topn,size_top,sizeof(item),cmp);
  106. //output
  107. fout<<topn[0].freq<<endl;
  108. fout<<Int2String(topn[0].num,topn[0].len);
  109.  
  110. int n = 1;
  111. int perline = 1;
  112. for (int i = 1 ; i<size_top ;i++)
  113. {
  114. if (topn[i].freq < topn[i-1].freq)
  115. {
  116. perline= 1;
  117.  
  118. n++;
  119. fout<<endl;
  120. if (n==N+1||topn[i].freq==0)
  121. {
  122. break;
  123. }
  124. fout<<topn[i].freq<<endl;
  125. fout<<Int2String(topn[i].num,topn[i].len);
  126.  
  127. }
  128. else
  129. {
  130. if (perline%6==0)
  131. {
  132. fout<<endl;
  133. perline = 0;
  134. }
  135. else
  136. fout<<' ';
  137. fout<<Int2String(topn[i].num,topn[i].len);
  138. perline++;
  139. }
  140. }
  141. fin.close();
  142. fout.close();
  143. return 0;
  144. }
Add Comment
Please, Sign In to add comment