Guest User

Untitled

a guest
Nov 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. private int[] calculatePrefSuffArray(String pattern) {
  2. char patternArray[] = pattern.toCharArray();
  3. int tab[] = new int[pattern.length()];
  4. tab[0] = 0;
  5. int t = tab[0];
  6. for (int i = 1; i < pattern.length(); i++) { // t = tab[i-1]
  7. while(t >0 && patternArray[i] != patternArray[t] ) {
  8. t = tab[t-1];
  9. }
  10. if(patternArray[i] == patternArray[t]) t++;
  11. tab[i] = t;
  12. }
  13. return tab;
  14. }
  15.  
  16. #include<iostream>
  17. #include<fstream>
  18. #include<cstdlib>
  19.  
  20. using namespace std;
  21.  
  22. main()
  23. {
  24. ofstream myfile("inputtext.txt");
  25. char *intxt="abcd"; // I wish the sample text to contain only these alphabets
  26. long int i=0;
  27. char* txt = new char[10000000];
  28. while(i<10000000)
  29. {
  30. txt[i]=intxt[(rand()%4)];
  31. i++;
  32. }
  33. myfile<<txt;
  34. myfile.close();
  35. }
  36.  
  37. #include<iostream>
  38. #include<string>
  39. #include<fstream>
  40. using namespace std;
  41.  
  42. void prefix_fn(int* p, char* s)
  43. {
  44. s--;
  45. p--;
  46. int i,k=0;
  47. p[1]=0;
  48. for(i=2;i<=7;i++)
  49. {
  50. while ((k>0)&&(s[k+1]!=s[i]))
  51. k=p[k];
  52. if (s[k+1]==s[i])
  53. k++;
  54. p[i]=k;
  55. }
  56. }
  57. main()
  58. {
  59. int i,k;
  60. int *p= new int[7];
  61. char* txt=new char[10000000];
  62. ifstream myfile("inputtext.txt");
  63. myfile.seekg(0,ios::beg);
  64.  
  65. myfile.read(txt,10000000);
  66. myfile.close();
  67. char *s="abdcbab";
  68. cout<<"Prefix Table"<<endl;
  69. prefix_fn(p,s);
  70. p--; // This helps to start indexing from 1, just for me to avoid confusion
  71. s--; // Similar objective as stated above.
  72. for(int i=1;i<=7;i++)
  73. cout<<p[i]<<endl;
  74. txt--;
  75. k=0;
  76. for(i=1;i<=10000000;i++)
  77. {
  78. while(k>0 && s[k+1]!=txt[i])
  79. {
  80. k=p[k];
  81. }
  82. if(s[k+1]==txt[i])
  83. {
  84. k=k+1;
  85. }
  86. if(k==7)
  87. {
  88. cout<<"Match found at position : "<<i-6<<endl;
  89. k=p[k];
  90. }
  91. }
  92. p++;
  93. delete[] p;
  94. txt++;
  95. delete[] txt; //deleting the memory allocated.
  96. }
  97.  
  98. [0 0 0 0 1 2 3 0 1 2 0 1 2 3 0 1 2 3 0 1 2 0 0]
  99.  
  100. [-1 0 0 0 1 2 3 0 1 2 0 1 2 3 0 1 2 3 0 1 2 0 0 ]
  101.  
  102. tab[0] = 0;
  103. int t = tab[0];
  104.  
  105. tab[0] = -1;
  106. tab[1] = 0;
  107. int t = tab[1];
  108.  
  109. private boolean search(String string, String word, int[] table) {
  110. int m = 0, i = 0;
  111. while ((m + i) < string.length()) {
  112. if (word.charAt(i) == string.charAt(m + i)) {
  113. if (i == word.length() - 1) {
  114. return true;
  115. }
  116. i++;
  117. } else {
  118. if (table[i] > -1) {
  119. m = m + i - table[i];
  120. i = table[i];
  121. } else {
  122. i = 0;
  123. m++;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129.  
  130. class Solution(object):
  131. def strStr(self, haystack, needle):
  132. """
  133. :type haystack: str
  134. :type needle: str
  135. :rtype: int
  136. """
  137.  
  138. if not needle:
  139. return 0
  140.  
  141. q = 0
  142. preList = self.calPre(needle)
  143. # print preList
  144. for i in xrange(len(haystack)):
  145. # print needle[q], haystack[i], i
  146. while q>0 and needle[q] != haystack[i]: # 加入循环,让累加子串充分比较,直到q变成0,防止haystack中有元素未参与可能相等的比较
  147. q = preList[q]
  148. if needle[q] == haystack[i]:
  149. q += 1
  150. if q == len(needle):
  151. return i - len(needle) + 1 # 比较完毕
  152. return -1
  153.  
  154. def calPre(self, s):
  155. preList = [0, 0]
  156. i = 1
  157. j = 0
  158. while i < len(s):
  159. if s[i] == s[j]: # 进行子串累加匹配
  160. preList.append(j+1)
  161. i += 1
  162. j += 1
  163. elif s[i] == s[0]: # 重新开始子串匹配
  164. j = 0
  165. else: # 无法匹配
  166. preList.append(0)
  167. i += 1
  168. j = 0
  169. return preList
Add Comment
Please, Sign In to add comment