Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ABYSS CONTINUE
- *
- * @author Lulu Ilmaknun Qurotaini
- * with help of:
- * Dhipta Raditya Yudhasmara
- */
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- public class SDA1819T4 {
- static int[] power = new int[1003];
- static int base = 29;
- static Word[] hashtable;
- static int size = 8699;
- static String S_g;
- public static void main(String args[]) throws IOException{
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- power[0] = 1;
- for(int i = 1; i < 1000; i++)
- power[i] = power[i - 1] * base;
- String S = reader.readLine();
- S_g = S;
- int Q = Integer.parseInt(reader.readLine());
- for(int i=0; i < Q; i++){
- String[] code = reader.readLine().split(" ");
- int type = Integer.parseInt(code[0]);
- if(type == 0){
- type0(S, code[1]);
- }else if(type == 1){
- type1(S);
- }
- }
- }
- static int getHash(String word, int len){
- int hash = 0;
- for(int i = 0; i < len; i++){
- // System.out.println("loop2");
- hash += ((int) word.charAt(i) - 96) * power[len - 1 - i];
- }
- return hash;
- }
- static Word cari(int hash){
- int index_hash = Math.floorMod(hash, size);
- Word word = hashtable[index_hash];
- while(word.hash != hash){
- // System.out.println("loop3 " + S_g.substring(word.start, word.end) + " " + hash + " " + word.hash);
- index_hash += word.col * word.col;
- index_hash = Math.floorMod(index_hash, size);
- word = hashtable[index_hash];
- }
- return word;
- }
- static void type0(String S, String k){
- int count = 0;
- int len = k.length();
- int hash = getHash(k, len);
- String sample = S.substring(0, len);
- int current_hash = getHash(sample, len);
- if(current_hash == hash)
- count++;
- for(int j = 0; j < S.length() - len; j++){
- current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
- current_hash *= base;
- current_hash += ((int) S.charAt(j + len) - 96);
- if(current_hash == hash)
- count++;
- }
- System.out.println(count);
- }
- static void type1(String S){
- for(int len = 1; len < S.length(); len++){
- // System.out.println("loop1");
- hashtable = new Word[size];
- Word max = null;
- int awal = 0;
- int akhir = 1;
- String sample = S.substring(0,len);
- int sample_hash = getHash(sample, len);
- int hash = sample_hash;
- int current_hash = Math.floorMod(hash, size);
- hashtable[current_hash] = new Word(hash,0, len);
- for(int j = 0; j < S.length() - len; j++){
- // System.out.println("loop11");
- current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
- current_hash *= base;
- current_hash += (int) S.charAt(j + len) - 96;
- // System.out.println(current_hash + " " + S_g.substring(j+1, j+1+len));
- hash = Math.floorMod(current_hash, size);
- if(hashtable[hash] != null) {
- if(current_hash == hashtable[hash].hash){
- hashtable[hash].count++;
- if(max != null){
- if(hashtable[hash].count > max.count)
- max = hashtable[hash];
- }
- } else{
- while(hashtable[hash] != null){
- // System.out.println("loop111");
- hashtable[hash].col++;
- hash += (hashtable[hash].col)*(hashtable[hash].col);
- }
- hashtable[hash] = new Word(current_hash,j+1, j+1+len);
- if(max == null)
- max = hashtable[hash];
- }
- } else{
- Word new_word = new Word(current_hash,j+1, j+1+len);
- hashtable[hash] = new_word;
- if(max == null)
- max = hashtable[hash];
- }
- // System.out.println(hash + " " + current_hash + " " + S_g.substring(hashtable[hash].start, hashtable[hash].end) + " " + hashtable[hash].count);
- }
- // for(int j=0; j < size; j++){
- // if(hashtable[j] != null)
- // System.out.println(j + " " + S_g.substring(hashtable[j].start, hashtable[j].end) + " " + hashtable[j].count);
- // }
- // Word current_word = cari(sample_hash);
- // max = current_word;
- // current_hash = Math.floorMod(current_word.hash, size);
- //
- // for(int j = 0; j < S.length() - len; j++){
- // // System.out.println("loop12");
- // current_hash -= ((int) S.charAt(j) - 96) * power[len - 1];
- // current_hash *= base;
- // current_hash += (int) S.charAt(j + len) - 96;
- //
- // current_word = cari(current_hash);
- // if (current_word.count > max.count){
- // max = current_word;
- // }
- // }
- System.out.println(S.substring(max.start, max.end) + " " + max.count);
- }
- System.out.println(S + " 1");
- }
- }
- class Word{
- int hash;
- int start;
- int end;
- int count;
- int col;
- Word(int hash, int start, int end){
- this.hash = hash;
- this.start = start;
- this.end = end;
- count = 1;
- col = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement