Source code for submission s639

ololo.cpp

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <memory.h>
  6. #include <cstring>
  7. #include <string>
  8.  
  9.  
  10. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  11. #define FI(i,b) FOR(i,0,b)
  12. #define V(t) vector < t >
  13. #define pb push_back
  14. #define MEMS(a,b) memset((a),(b),sizeof(a))
  15. #define U unsigned
  16. #define LL long long
  17. using namespace std;
  18.  
  19. U LL p[2000010];
  20. U LL h[2000010];
  21. char s[1010];
  22. char cur[2000010];
  23. char st[2000010];
  24. int main()
  25. {
  26. p[0]=1;
  27. FOR(i,1,2000010)
  28. p[i]=p[i-1]*257;
  29. int n;
  30. while (scanf("%d%s",&n,&s)!=EOF)
  31. {
  32. gets(cur);
  33. //printf("DEBUG\n");
  34. //printf("%s\n",s);
  35. U LL gh=0;
  36. int bm=strlen(s);
  37. FOR(i,0,bm)
  38. gh+=p[i]*((unsigned int)s[i]);
  39. FOR(i,0,n)
  40. {
  41. gets(cur);
  42. int m=strlen(cur);
  43. int top=0;
  44. FOR(j,0,m)
  45. {
  46. U LL prev=0;
  47. if (top)
  48. prev=h[top-1];
  49. st[top]=cur[j];
  50. h[top]=prev+p[top]*((unsigned int)cur[j]);
  51. top++;
  52. if (top>=bm)
  53. {
  54. U LL curh=h[top-1];
  55. if (top>bm)
  56. curh-=h[top-1-bm];
  57. U LL mn=1;
  58. if (curh==gh*p[top-bm])
  59. top-=bm;
  60. }
  61. }
  62. st[top]=0;
  63. printf("%s\n",st);
  64. }
  65. }
  66. return 0;
  67. }