a53

Catchy

a53
Sep 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <fstream>
  2. #include <cassert>
  3. #include <cstring>
  4. #define DMAX 120000
  5. #define LGMAX 26
  6. #define LMAX 1504
  7.  
  8. using namespace std;
  9. ifstream fin("catchy.in");
  10. ofstream fout("catchy.out");
  11.  
  12. struct cuvant {char c[LGMAX]; int nr; };
  13. struct icuvant {int poz, nr;};
  14. cuvant D[DMAX];
  15. icuvant B[LMAX/2];
  16. int nrd, K, nrb, nrmax, N, exmax;
  17.  
  18. char s[LMAX];
  19. char sep[]=":,;)(.?!><}{ ";
  20.  
  21. int cauta(char *p);
  22. void insereaza (char *p, int poz);
  23. int cautaB(int p);
  24. void insereazaB (int poz, int Bpoz);
  25.  
  26. int main()
  27. {int i, j, buna, minim, poz, Bpoz;
  28. char *p;
  29. fin>>K; fin.get();
  30. for (i=0; i<K; i++)
  31. {fin.getline(s,LMAX);
  32. p=strtok(s,sep);
  33. while (p)
  34. {poz=cauta(p);
  35. if (poz==nrd || strcmp(D[poz].c,p))
  36. insereaza(p, poz);
  37. else
  38. D[poz].nr++;
  39. p=strtok(NULL,sep);
  40. }
  41. }
  42. fin>>N; fin.get();
  43. for (i=1; i<=N; i++)
  44. {fin.getline(s,LMAX);
  45. p=strtok(s,sep);
  46. buna=1; nrb=0;
  47. while (p)
  48. {poz=cauta(p);
  49. if (poz==nrd || strcmp(D[poz].c,p))
  50. {buna=0; break;}
  51. Bpoz=cautaB(poz);
  52. if (Bpoz==nrb || B[Bpoz].poz!=poz)
  53. insereazaB(poz, Bpoz);
  54. else
  55. B[Bpoz].nr++;
  56. p=strtok(NULL,sep);
  57. }
  58.  
  59. if (buna)
  60. {minim=DMAX+1;
  61. for (j=0; j<nrb; j++)
  62. if (D[B[j].poz].nr/B[j].nr<minim)
  63. minim=D[B[j].poz].nr/B[j].nr;
  64. if (minim>exmax) {exmax=minim; nrmax=i;}
  65. }
  66. }
  67.  
  68. fout<<nrmax<<'\n'<<exmax<<'\n';
  69. fout.close();
  70. return 0;
  71. }
  72.  
  73. int cauta(char *p)
  74. {int st=-1, dr=nrd, mijloc;
  75. while (dr-st>1)
  76. {
  77. mijloc=(st+dr)/2;
  78. if (strcmp(D[mijloc].c, p)<0) st=mijloc;
  79. else dr=mijloc;
  80. }
  81. return dr;
  82. }
  83.  
  84. void insereaza (char *p, int poz)
  85. {int i;
  86. for (i=nrd; i>poz; i--) D[i]=D[i-1];
  87. D[poz].nr=1;
  88. strcpy(D[poz].c,p);
  89. nrd++;
  90. }
  91.  
  92.  
  93. int cautaB(int p)
  94. {int st=-1, dr=nrb, mijloc;
  95. while (dr-st>1)
  96. {
  97. mijloc=(st+dr)/2;
  98. if (B[mijloc].poz<p) st=mijloc;
  99. else dr=mijloc;
  100. }
  101. return dr;
  102. }
  103.  
  104. void insereazaB (int poz, int Bpoz)
  105. {int i;
  106. for (i=nrb; i>Bpoz; i--) B[i]=B[i-1];
  107. B[Bpoz].nr=1;
  108. B[Bpoz].poz=poz;
  109. nrb++;
  110. }
Add Comment
Please, Sign In to add comment