Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. #define p 5
  9. int pp[5005];
  10. int hash[5005];
  11. int kol = 0;
  12. int hsh[5005];
  13.  
  14. char buf[5005];
  15.  
  16. char f(char c){
  17. switch(c){
  18. case 'A':
  19. return 1;
  20. case 'C':
  21. return 2;
  22. case 'G':
  23. return 3;
  24. case 'T':
  25. return 4;
  26. }
  27. return -1;
  28. }
  29.  
  30. int main() {
  31. freopen("input.txt", "r", stdin);
  32. freopen("output.txt", "w", stdout);
  33. cin >> buf;
  34. int n = strlen(buf);
  35. pp[0] = 1;
  36. for (int i=1; i<5005; i++){
  37. pp[i] = pp[i-1] * p;
  38. }
  39. hsh[0] = 0;
  40. for (int i=0; i<n; i++){
  41. buf[i] = f(buf[i]);
  42. hsh[i+1] = buf[i] + hsh[i] * p;
  43. }
  44. int ret = 0;
  45. for (int len=1; len<n; len++){
  46. int kol = 0;
  47. for (int i=0; i<n-len+1; i++){
  48. hash[kol++] = hsh[i+len] - hsh[i] * pp[len];
  49. }
  50. sort(hash, hash + kol);
  51. int ps = -1;
  52. for (int i=1; i<kol; i++){
  53. if (hash[i] == hash[i-1]){
  54. if (ps == -1 || hash[i] != hash[ps]){
  55. ps = i;
  56. ret++;
  57. }
  58. }
  59. }
  60. }
  61. printf("%d\n%lf", ret, clock());
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement