Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<set>
  4. #define LL long long
  5.  
  6. using namespace std;
  7. int N;
  8. int in[50010];
  9. long long hash[50010];
  10. LL base=31;
  11. long long modulo=9223372036854775807;
  12.  
  13.  
  14. bool step(int k)
  15. {
  16. set<LL> sset;
  17. if(k==1)
  18. {
  19. for(int i=0;i<N;i++)
  20. {
  21. if(sset.find(in[i])!=sset.end()) return false;
  22. sset.insert(in[i]);
  23.  
  24. }
  25. return true;
  26. }
  27.  
  28.  
  29. //init hash
  30. LL ihash=0;
  31. LL d=base;
  32. for(int i=0;i<k;i++)
  33. {
  34. ihash=(base) * (ihash) + in[i];
  35. ihash=ihash%modulo;
  36. if(i<k-2) d=d*base;
  37. d=d%modulo;
  38. }
  39. hash[0]=ihash;
  40. sset.insert(ihash);
  41. //printf("i=%lld d %lld\n",ihash,d);
  42.  
  43. for(int i=1;i<=N-k;i++)
  44. {
  45.  
  46. hash[i]=hash[i-1]-d*in[i-1];
  47. hash[i]=hash[i]*base;
  48. hash[i]=hash[i]+in[i+k-1];
  49. //printf("H[%d]=%lld\n",i,hash[i]);
  50. if(sset.find(hash[i])!=sset.end()) return false;
  51. sset.insert(hash[i]);
  52. }
  53. return true;
  54. }
  55.  
  56. int main()
  57. {
  58. int Z;
  59. scanf("%d",&Z);
  60. while(Z--)
  61. {
  62. scanf("%d",&N);
  63. for(int i=0;i<N;i++)
  64. scanf("%d",&in[i]);
  65.  
  66. /* for(int i=0;i<N;i++)
  67. {
  68. printf("step(%d)=%d\n",i,step(i));
  69.  
  70. }*/
  71.  
  72. int p=0, q=N,s;
  73. while(p!=q)
  74. {
  75.  
  76. s=(p+q+1)/2;
  77. //printf("%d\n",s);
  78. if(step(s))
  79. q=s-1;
  80. else
  81. p=s;
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. }
  90. if(s==N) printf("%d\n",N); else
  91. printf("%d\n",s+1);
  92. }
  93.  
  94. return 0;
  95. }
Add Comment
Please, Sign In to add comment