Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- import java.util.*;
- //inspired by LWT TARUNG by ridho dan indra dan dhipta r. jadi kemungkinan algo nya sama tp ngoding sendiri kok
- //juga fadhlannnn
- public class SDA1819T4{
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String kata = reader.readLine();
- int manycodes = Integer.parseInt(reader.readLine());
- for (int i = 0; i < manycodes; i++) {
- String[] event = reader.readLine().split(" ");
- String operation = event[0];
- if(operation.equals("0")){
- String subkata = event[1];
- System.out.println(hashZero(kata, subkata));
- }
- else if(operation.equals("1")){
- hashOne(kata);
- }
- }
- }
- // static int chartoInt(char c){
- // return ((int) c) - ((int) 'a');
- // }
- static long base = 31;
- static long[] store = new long[10050];
- static void preCompute(){
- store[0] = 1;
- for (int i = 1; i<=1000; i++){
- store[i] = store[i-1]*base;
- }
- }
- static long getHash(String word){
- int lengnya = word.length();
- long hash = 0;
- for (int i = 0; i<lengnya; i++){
- hash = hash + word.charAt(i)*store[lengnya-1-i];
- }
- return hash;
- }
- static int base2 = 31;
- static int[] store2 = new int[1550];
- static int[] table = new int[1543];
- static void preCompute2(){
- store2[0] = 1;
- for (int i = 1; i<=1000; i++){
- store2[i] = store2[i-1]*base2;
- }
- for (int i = 0; i<=1000; i++){
- table[i] = -1;
- }
- }
- static int getHash2(String word){
- int lengnya = word.length();
- int hash = 0;
- for (int i = 0; i<lengnya; i++){
- hash = hash + word.charAt(i)*store2[lengnya-1-i];
- }
- return hash;
- }
- static long hashZero(String text, String pattern) {
- preCompute();
- int total = 0;
- int T = text.length();
- int P = pattern.length();
- long hashpattern = getHash(pattern);
- long hashcur = getHash(text.substring(0,P));
- if(hashcur == hashpattern){
- total+=1;
- }
- for(int i = 0; i<T-P; i++){
- hashcur-=text.charAt(i)*store[P-1];
- hashcur*=base;
- hashcur+=text.charAt(i+P);
- if(hashcur == hashpattern){
- total+=1;
- }
- }
- return total;
- }
- static void hashOne(String text){
- preCompute2();
- int fre = 0;
- int T = text.length();
- int[] used = new int[1000];
- int max = 0;
- // ArrayList<Integer> used = new ArrayList<Integer>();
- //nge for buat substring panjang 1, 2,3
- for (int leng = 1; leng <= T; leng++){
- // System.out.println("lenggg " + leng);
- int hashcur = getHash2(text.substring(0,leng));
- int tmp = 0;
- hashcur = Math.floorMod(hashcur, 1543);
- table[hashcur] = 0;
- int index = 0;
- String terpendek = "";
- for(int i = 0; i<T-leng; i++){
- index ++;
- hashcur-=text.charAt(i)*store[leng-1];
- hashcur*=base;
- hashcur+=text.charAt(i+leng-1);
- hashcur = Math.floorMod(hashcur, 1543);
- String benar = text.substring(i+1,leng+i+1);
- tmp = hashcur;
- while (table[tmp] != -1){
- // if(table[hashcur]+leng > T){
- // hashcur = Math.floorMod(hashcur, 1543);
- // }
- String masuk = text.substring(table[tmp],table[tmp]+leng);
- if(!(benar.equals(masuk))){
- tmp++;
- tmp = Math.floorMod(tmp, 1543);
- } else {
- System.out.println("masuk");
- if (used[index] == 0){
- used[index]=1;
- }else{
- if(benar.equals(masuk)){
- used[table[tmp]] = used[table[tmp]] + 1;//kalo sama kan masuknya kesini. jd gini?
- }
- }
- }
- tmp++;
- tmp = Math.floorMod(tmp, 1543);
- }
- table[tmp] = index;
- }
- for(int m =0; m<T-leng+1;m++){
- if(used[m]!=0){
- if(used[m]>max){
- max = used[m];
- terpendek = text.substring(m, m+leng);
- }
- }
- }
- System.out.println(terpendek + " " + max);
- //reset table
- for(int i = 0; i<1000; i++){
- table[i]= -1;
- used[i]=0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement