Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MostFrequentSubstring {
- public static void main (String[] args) throws IOException {
- CBHT<String,Integer> tabela = new CBHT<String,Integer>(300);
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String word = br.readLine().trim();
- String pom;
- String w;
- /*
- *
- * Vashiot kod tuka....
- *
- */
- for(int i=1; i<=word.length(); i++) {
- for(int j=0; j<=word.length()-i; j++) {
- pom=word.substring(j, j+i);
- if(tabela.search(pom)==null) {
- tabela.insert(pom, 1);
- }
- else
- {
- int val=tabela.search(pom).element.value;
- val++;
- tabela.insert(pom, val);
- }
- }
- }
- //System.out.println(tabela);
- int max=0;
- String maxs="";
- for(int i=1; i<=word.length(); i++) {
- for(int j=0; j<=word.length()-i; j++) {
- w=word.substring(j, j+i);
- int value = tabela.search(w).element.value;
- if(value>max)
- {
- max=value;
- maxs=tabela.search(w).element.key;
- }
- else if (value == max) {
- if (w.length() > maxs.length())
- {
- maxs=w;
- }
- else if (w.length() == maxs.length())
- {
- if (w.compareTo(maxs) < 0) {
- maxs = w;
- }
- }
- }
- }}
- System.out.println(maxs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement