Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. //inspired by LWT TARUNG by ridho dan indra dan dhipta r. jadi kemungkinan algo nya sama tp ngoding sendiri kok
  7. //juga fadhlannnn
  8. public class SDA1819T4{
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11. String kata = reader.readLine();
  12. int manycodes = Integer.parseInt(reader.readLine());
  13. for (int i = 0; i < manycodes; i++) {
  14. String[] event = reader.readLine().split(" ");
  15. String operation = event[0];
  16. if(operation.equals("0")){
  17. String subkata = event[1];
  18. System.out.println(hashZero(kata, subkata));
  19. }
  20. else if(operation.equals("1")){
  21. hashOne(kata);
  22. }
  23. }
  24. }
  25.  
  26. // static int chartoInt(char c){
  27. // return ((int) c) - ((int) 'a');
  28. // }
  29.  
  30. static long base = 31;
  31. static long[] store = new long[10050];
  32.  
  33. static void preCompute(){
  34. store[0] = 1;
  35. for (int i = 1; i<=1000; i++){
  36. store[i] = store[i-1]*base;
  37. }
  38. }
  39.  
  40. static long getHash(String word){
  41. int lengnya = word.length();
  42. long hash = 0;
  43. for (int i = 0; i<lengnya; i++){
  44. hash = hash + word.charAt(i)*store[lengnya-1-i];
  45. }
  46. return hash;
  47. }
  48.  
  49. static int base2 = 31;
  50. static int[] store2 = new int[1550];
  51. static int[] table = new int[1543];
  52.  
  53. static void preCompute2(){
  54. store2[0] = 1;
  55. for (int i = 1; i<=1000; i++){
  56. store2[i] = store2[i-1]*base2;
  57. }
  58. for (int i = 0; i<=1000; i++){
  59. table[i] = -1;
  60. }
  61. }
  62.  
  63. static int getHash2(String word){
  64. int lengnya = word.length();
  65. int hash = 0;
  66. for (int i = 0; i<lengnya; i++){
  67. hash = hash + word.charAt(i)*store2[lengnya-1-i];
  68. }
  69. return hash;
  70. }
  71.  
  72. static long hashZero(String text, String pattern) {
  73. preCompute();
  74. int total = 0;
  75. int T = text.length();
  76. int P = pattern.length();
  77. long hashpattern = getHash(pattern);
  78. long hashcur = getHash(text.substring(0,P));
  79.  
  80. if(hashcur == hashpattern){
  81. total+=1;
  82. }
  83.  
  84. for(int i = 0; i<T-P; i++){
  85. hashcur-=text.charAt(i)*store[P-1];
  86. hashcur*=base;
  87. hashcur+=text.charAt(i+P);
  88. if(hashcur == hashpattern){
  89. total+=1;
  90. }
  91. }
  92. return total;
  93. }
  94.  
  95. static void hashOne(String text){
  96. preCompute2();
  97. int fre = 0;
  98. int T = text.length();
  99. int[] used = new int[1000];
  100. int max = 0;
  101. // ArrayList<Integer> used = new ArrayList<Integer>();
  102. //nge for buat substring panjang 1, 2,3
  103. for (int leng = 1; leng <= T; leng++){
  104. // System.out.println("lenggg " + leng);
  105. int hashcur = getHash2(text.substring(0,leng));
  106. int tmp = 0;
  107. hashcur = Math.floorMod(hashcur, 1543);
  108. table[hashcur] = 0;
  109. int index = 0;
  110. String terpendek = "";
  111. for(int i = 0; i<T-leng; i++){
  112. index ++;
  113. hashcur-=text.charAt(i)*store[leng-1];
  114. hashcur*=base;
  115. hashcur+=text.charAt(i+leng-1);
  116. hashcur = Math.floorMod(hashcur, 1543);
  117. String benar = text.substring(i+1,leng+i+1);
  118. tmp = hashcur;
  119. while (table[tmp] != -1){
  120. // if(table[hashcur]+leng > T){
  121. // hashcur = Math.floorMod(hashcur, 1543);
  122. // }
  123. String masuk = text.substring(table[tmp],table[tmp]+leng);
  124. if(!(benar.equals(masuk))){
  125. tmp++;
  126. tmp = Math.floorMod(tmp, 1543);
  127. } else {
  128. System.out.println("masuk");
  129. if (used[index] == 0){
  130. used[index]=1;
  131. }else{
  132. if(benar.equals(masuk)){
  133. used[table[tmp]] = used[table[tmp]] + 1;//kalo sama kan masuknya kesini. jd gini?
  134. }
  135. }
  136. }
  137. tmp++;
  138. tmp = Math.floorMod(tmp, 1543);
  139. }
  140. table[tmp] = index;
  141. }
  142. for(int m =0; m<T-leng+1;m++){
  143. if(used[m]!=0){
  144. if(used[m]>max){
  145. max = used[m];
  146. terpendek = text.substring(m, m+leng);
  147. }
  148. }
  149. }
  150. System.out.println(terpendek + " " + max);
  151. //reset table
  152. for(int i = 0; i<1000; i++){
  153. table[i]= -1;
  154. used[i]=0;
  155. }
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement