Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6.  
  7. int moduloPower(int first, int second, int modulus) {
  8. int result = 1;
  9. long tmp = first%modulus;
  10.  
  11. for (int i = 1; i <= second; i <<= 1) {
  12. tmp %= modulus;
  13. if ((second&i) != 0) {
  14. result *= tmp;
  15. result %= modulus;
  16. }
  17. tmp *= tmp;
  18. }
  19.  
  20. return result%modulus;
  21. }
  22.  
  23.  
  24. int main() {
  25.  
  26. int totalNumberOfCharacters = 256;
  27. int primeNumber = 101;
  28.  
  29. int numberOfFiles;
  30. int patternLenght, textLenght, hash1, hash2, rm, i, j;
  31. std::string downloadedString;
  32. std::string tmpPattern;
  33. std::string tmpFileName;
  34. std::string tmpText;
  35. std::vector<std::string> vectorFiles;
  36. std::vector<std::string> vectorPatterns;
  37. std::ifstream file;
  38.  
  39. std::cin >> downloadedString;
  40. try {
  41. numberOfFiles = stoi(downloadedString);
  42. }
  43. catch (...) {
  44. std::cout << "fail to convert value (numberOfFiles)" << std::endl;
  45. return EXIT_FAILURE;
  46. }
  47. for (int i = 0; i < numberOfFiles; i++) {
  48. std::cin >> downloadedString;
  49. vectorFiles.push_back(downloadedString);
  50. std::cin >> downloadedString;
  51. vectorPatterns.push_back(downloadedString);
  52. }
  53.  
  54.  
  55.  
  56.  
  57. for (int counter = 0; counter < numberOfFiles; counter++) {
  58. tmpPattern = vectorPatterns[counter];
  59. std::cout << tmpPattern << std::endl;
  60. tmpFileName = vectorFiles[counter];
  61. std::cout << tmpFileName << std::endl;
  62. file.open(tmpFileName, std::ios::in);
  63. if (file.good() == true)
  64. {
  65. tmpText.assign((std::istreambuf_iterator<char>(file)),
  66. (std::istreambuf_iterator<char>()));
  67. file.seekg(0, std::ios_base::beg);
  68. file.close();
  69. }
  70. else std::cout << "Cannot open file: " << tmpFileName << std::endl;
  71.  
  72. textLenght = tmpText.length();
  73. patternLenght = tmpPattern.length();
  74. hash2 = 0;
  75. hash1 = 0;
  76.  
  77. for (i = 0; i<patternLenght; i++)
  78. {
  79. hash2 = ((hash2*totalNumberOfCharacters) + tmpText[i]);
  80. hash2 %= primeNumber;
  81. }
  82.  
  83. for (i = 0; i<patternLenght; i++)
  84. {
  85. hash1 = ((hash1*totalNumberOfCharacters) + tmpText[i]);
  86. hash1 %= primeNumber;
  87. }
  88.  
  89. rm = moduloPower(totalNumberOfCharacters, patternLenght - 1, primeNumber);
  90. i = 0;
  91. while (i < textLenght - patternLenght)
  92. {
  93. j = 0;
  94. if (hash1 == hash2) while ((j<patternLenght) && (tmpPattern[j] == tmpText[i + j])) j++;
  95. if (j == patternLenght) {
  96. std::cout << i << " ";
  97. }
  98. hash2 = ((hash2 - tmpText[i] * rm)*totalNumberOfCharacters + tmpText[i + patternLenght]);
  99. hash2 %= primeNumber;
  100. if (hash2 < 0) {
  101. hash2 += primeNumber;
  102. }
  103. i++;
  104. }
  105. j = 0;
  106. if (hash1 == hash2) {
  107. while ((j < patternLenght) && (tmpPattern[j] == tmpText[i + j])) j++;
  108. }
  109. if (j == patternLenght) {
  110. std::cout << i << " ";
  111. }
  112. std::cout << std::endl;
  113. }
  114.  
  115.  
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement