Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Фреквентен стринг Problem 3 (0 / 1)
- Даден е стринг. Треба да се најде најчестиот под-стринг кој е дел од него и да се испечати. Доколку два под-стринга се исто фреквентни, тогаш се печати подолгиот. Доколку и овој услов го исполнуваат тогаш се печати лексикографски помалиот.
- Пример: За стрингот "abc" под-стрингови се "a", "b", "c", "ab", "bc", "abc". Сите имаат иста честота па затоа се печати најдолгиот "abc".
- Име на класата (Java): MostFrequentSubstring.
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- K key;
- E value;
- public MapEntry (K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo (K that) {
- @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "(" + key + "," + value + ")";
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- private SLLNode<MapEntry<K,E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K,E>> search(K targetKey) {
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- curr.element = newEntry;
- return;
- }
- }
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K,E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- 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();
- // Vashiot kod tuka....
- char []letters = word.toCharArray();
- // generates substring
- for( int i=1; i < letters.length; i++ ) { // determines substring lenght
- for (int j = 0; j < letters.length - i + 1; j++) { // determines substring beginning and puts them in hash
- String str = "";
- for (int k = j; k < j + i; k++) { // determines substrings
- str += Character.toString(letters[k]);
- }
- SLLNode<MapEntry<String, Integer>> node = tabela.search(str); // determines if the substring has been allready hashed
- if (node == null)
- tabela.insert(str, 1);
- else
- tabela.insert(str, (node.element.value + 1)); // enlarges the sub substring frequency by 1
- }
- } // done with hashing
- // looking for the most frequent string in the hash
- String mostFrequent = "";
- int frequencyMax = 0;
- for( int i=1; i < letters.length; i++ ) { // generates substrings, same as above
- for (int j = 0; j < letters.length - i + 1; j++) {
- String str = "";
- for (int k = j; k < j + i; k++) {
- str += Character.toString(letters[k]);
- }
- SLLNode<MapEntry<String, Integer>> node = tabela.search(str);
- if (node == null)
- continue;
- else {
- if ( node.element.value > frequencyMax ) {
- frequencyMax = node.element.value;
- mostFrequent = str;
- }
- else if( node.element.value == frequencyMax ) {
- if( str.length() > mostFrequent.length() ) {
- frequencyMax = node.element.value;
- mostFrequent = str;
- }
- else if( str.length() == mostFrequent.length() ) {
- if( lexikographicValue( str ) < lexikographicValue( mostFrequent ) ) {
- frequencyMax = node.element.value;
- mostFrequent = str;
- }
- }
- }
- }
- }
- }
- System.out.println( mostFrequent );
- }
- public static int lexikographicValue( String str ) {
- char letters[] = str.toCharArray();
- int val = 0;
- for( int i = 0; i < letters.length; i++ ) {
- val += (int)letters[i];
- }
- return val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement