Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. /**
  2. * ABYSS CONTINUE
  3. *
  4. * @author Lulu Ilmaknun Qurotaini
  5. * with help of:
  6. * Dhipta Raditya Yudhasmara
  7. */
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.io.IOException;
  12.  
  13. public class SDA1819T4 {
  14. static int[] power = new int[1003];
  15. static int base = 29;
  16. static Word[] hashtable;
  17. static int size = 8699;
  18. static String S_g;
  19.  
  20. public static void main(String args[]) throws IOException{
  21. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  22.  
  23. power[0] = 1;
  24. for(int i = 1; i < 1000; i++)
  25. power[i] = power[i - 1] * base;
  26.  
  27. String S = reader.readLine();
  28. S_g = S;
  29. int Q = Integer.parseInt(reader.readLine());
  30. for(int i=0; i < Q; i++){
  31. String[] code = reader.readLine().split(" ");
  32. int type = Integer.parseInt(code[0]);
  33.  
  34. if(type == 0){
  35. type0(S, code[1]);
  36. }else if(type == 1){
  37. type1(S);
  38. }
  39. }
  40. }
  41.  
  42. static int getHash(String word, int len){
  43. int hash = 0;
  44. for(int i = 0; i < len; i++){
  45. // System.out.println("loop2");
  46. hash += ((int) word.charAt(i) - 96) * power[len - 1 - i];
  47. }
  48. return hash;
  49. }
  50.  
  51. static Word cari(int hash){
  52. int index_hash = Math.floorMod(hash, size);
  53. Word word = hashtable[index_hash];
  54.  
  55. while(word.hash != hash){
  56. // System.out.println("loop3 " + S_g.substring(word.start, word.end) + " " + hash + " " + word.hash);
  57. index_hash += word.col * word.col;
  58. index_hash = Math.floorMod(index_hash, size);
  59. word = hashtable[index_hash];
  60. }
  61.  
  62. return word;
  63. }
  64.  
  65. static void type0(String S, String k){
  66. int count = 0;
  67. int len = k.length();
  68.  
  69. int hash = getHash(k, len);
  70. String sample = S.substring(0, len);
  71. int current_hash = getHash(sample, len);
  72.  
  73. if(current_hash == hash)
  74. count++;
  75.  
  76. for(int j = 0; j < S.length() - len; j++){
  77. current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
  78. current_hash *= base;
  79. current_hash += ((int) S.charAt(j + len) - 96);
  80.  
  81. if(current_hash == hash)
  82. count++;
  83. }
  84.  
  85. System.out.println(count);
  86. }
  87.  
  88. static void type1(String S){
  89. for(int len = 1; len < S.length(); len++){
  90. // System.out.println("loop1");
  91. hashtable = new Word[size];
  92. Word max = null;
  93. int awal = 0;
  94. int akhir = 1;
  95.  
  96. String sample = S.substring(0,len);
  97. int sample_hash = getHash(sample, len);
  98. int hash = sample_hash;
  99. int current_hash = Math.floorMod(hash, size);
  100. hashtable[current_hash] = new Word(hash,0, len);
  101.  
  102. for(int j = 0; j < S.length() - len; j++){
  103. // System.out.println("loop11");
  104. current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
  105. current_hash *= base;
  106. current_hash += (int) S.charAt(j + len) - 96;
  107.  
  108. // System.out.println(current_hash + " " + S_g.substring(j+1, j+1+len));
  109. hash = Math.floorMod(current_hash, size);
  110. if(hashtable[hash] != null) {
  111. if(current_hash == hashtable[hash].hash){
  112. hashtable[hash].count++;
  113. if(max != null){
  114. if(hashtable[hash].count > max.count)
  115. max = hashtable[hash];
  116. }
  117. } else{
  118. while(hashtable[hash] != null){
  119. // System.out.println("loop111");
  120. hashtable[hash].col++;
  121. hash += (hashtable[hash].col)*(hashtable[hash].col);
  122. }
  123. hashtable[hash] = new Word(current_hash,j+1, j+1+len);
  124. if(max == null)
  125. max = hashtable[hash];
  126. }
  127. } else{
  128. Word new_word = new Word(current_hash,j+1, j+1+len);
  129. hashtable[hash] = new_word;
  130. if(max == null)
  131. max = hashtable[hash];
  132. }
  133. // System.out.println(hash + " " + current_hash + " " + S_g.substring(hashtable[hash].start, hashtable[hash].end) + " " + hashtable[hash].count);
  134.  
  135. }
  136.  
  137. // for(int j=0; j < size; j++){
  138. // if(hashtable[j] != null)
  139. // System.out.println(j + " " + S_g.substring(hashtable[j].start, hashtable[j].end) + " " + hashtable[j].count);
  140. // }
  141.  
  142. // Word current_word = cari(sample_hash);
  143. // max = current_word;
  144. // current_hash = Math.floorMod(current_word.hash, size);
  145. //
  146. // for(int j = 0; j < S.length() - len; j++){
  147. // // System.out.println("loop12");
  148. // current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
  149. // current_hash *= base;
  150. // current_hash += (int) S.charAt(j + len) - 96;
  151. //
  152. // current_word = cari(current_hash);
  153. // if (current_word.count > max.count){
  154. // max = current_word;
  155. // }
  156. // }
  157.  
  158. System.out.println(S.substring(max.start, max.end) + " " + max.count);
  159. }
  160. System.out.println(S + " 1");
  161. }
  162. }
  163.  
  164. class Word{
  165. int hash;
  166. int start;
  167. int end;
  168. int count;
  169. int col;
  170.  
  171. Word(int hash, int start, int end){
  172. this.hash = hash;
  173. this.start = start;
  174. this.end = end;
  175. count = 1;
  176. col = 0;
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement