java - Algorithm seems to only work on last chunk of ArrayList -


the goal of algorithm take list of strings , abbreviate them in such way can uniquely identify word represent, homework assignment.

the algorithm i've tried create problem searches substrings possible matches, although seemed trie more effective solution, couldn't seem wrap head aroud how code 1 works; being said, should still possible way well.

input: ~~~~~~~~~~~ expected output: ~~~~~~~~~~~~ actual output:
carbohydrate -------- carboh -------------------------------- carbohydrate
cart -------------------- cart ------------------------------------ cart
carburetor ------------ carbu --------------------------------- carburetor
caramel --------------- cara ----------------------------------- caramel
caribou --------------- cari ------------------------------------- caribou
carbonic -------------- carboni -------------------------------- carbonic
cartilage -------------- carti ------------------------------------ cartilage
carbon ---------------- carbon --------------------------------- carbon
carriage -------------- carriage -------------------------------- carr
carton ----------------- carto ----------------------------------- carto
car --------------------- car ------------------------------------- car
carbonate ------------ carbona ------------------------------- carbona

this here actual code.

import java.util.arraylist;  public class shortestprefixes {      arraylist<string> charactercounter = new arraylist<>();     boolean isequal = false;      public static arraylist testarraylist()     {         arraylist<string> testarray = new arraylist<>();         testarray.add("carbohydrate");         testarray.add("cart");         testarray.add("carburetor");         testarray.add("caramel");         testarray.add("caribou");         testarray.add("carbonic");         testarray.add("cartilage");         testarray.add("carbon");         testarray.add("carriage");         testarray.add("carton");         testarray.add("car");         testarray.add("carbonate");         return testarray;      }     public arraylist getshortestprefixes(arraylist<string> input)     {         arraylist<string> output = new arraylist<>();         system.out.println(input.size());         (int count = 0; count<input.size(); count++) //this loop counts word being reduced         {             //system.out.println(count);             string word = input.get(count);             //system.out.println(word);             (int compare = 0; compare<input.size(); compare++)//this loop counts characters of word being reduced             {                      if(input.get(compare) .equals(word)){}                          isequal=false;                         arraylist<string> wordbeingcomparedrightnow = new arraylist<>();                         (int searchcount = 0; searchcount<input.get(searchcount).length(); searchcount++)//this loop compares string every possible substring                         {                             wordbeingcomparedrightnow.clear();                             string wordabouttobecompared = input.get(searchcount);                             wordbeingcomparedrightnow.add(wordabouttobecompared);                              (int miniloop = 0; miniloop<input.get(searchcount).length()-1; miniloop++)//this loop sets word's substrings tested                             {                                 wordabouttobecompared = wordabouttobecompared.substring(0, wordabouttobecompared.length() -1);                                 wordbeingcomparedrightnow.add(wordabouttobecompared);                              }                              for(int variablename = 0; variablename < wordbeingcomparedrightnow.size(); variablename++)//this word compares array craeated in last step substring have                             {                                 if(wordbeingcomparedrightnow.get(variablename) .equals(word.substring(0, word.length()-1)))                                 {                                     isequal=true;                                 }                              }                         }                             if (!isequal)                             {                             system.out.println(word);                             word = word.substring(0, word.length()-1);                             }              }             //system.out.println(word);             output.add(word);          }          system.out.println(testarraylist());         return output;      }      public static void main(string[] args)     {         shortestprefixes sp = new shortestprefixes();         arraylist<string> output = sp.getshortestprefixes(testarraylist());         system.out.println(output);      }  } 

i think problem somewhere within second loop, can't quite figure out where; if identify why algorithm doesn't work or @ least point me in right direction; i'd appreciate it. thanks.

if understand trying do, each word try find shortest prefix starting longest one.

then check if next prefix (the current 1 minus 1 character) equals of possible substrings of other words. note comparing prefix current word substrings, need skip current word in comparison.

if next prefix unique, becomes current one: word.substring(0, word.length()-1) process should repeated until can not find shorter prefix unique.

but for strange:

for (int searchcount = 0; searchcount < input.get(searchcount).length(); searchcount++) 

you iterate on searchcount it's part of both sides of end condition. end condition different each iteration.

since homework, i'll propose approach think more easy. idea start single-letter prefixes , enlarge them until valid.

step 0:     input <- [carbohydrate, cart, carburetor, caramel, caribou, carbonic, cartilage, carbon, carriage, carton, car, carbonate]     output <- [ , , , , , , , , , , , ]  step 1:      each prefix in output check if it's valid. (*1) if not add 1 more char it.   step 2:        if prefix not valid, go step 1.  (*1) prefix valid if 1 of folling conditions occurs: prefix unique in output or cannot add more chars prefix == word 

hint each iteration output is:

iteration 0: [, , , , , , , , , , , ] iteration 1: [c, c, c, c, c, c, c, c, c, c, c, c] iteration 2: [ca, ca, ca, ca, ca, ca, ca, ca, ca, ca, ca, ca] iteration 3: [car, car, car, car, car, car, car, car, car, car, car, car] iteration 4: [carb, cart, carb, cara, cari, carb, cart, carb, carr, cart, car, carb] iteration 5: [carbo, cart, carbu, cara, cari, carbo, carti, carbo, carr, carto, car, carbo] iteration 6: [carboh, cart, carbu, cara, cari, carbon, carti, carbon, carr, carto, car, carbon] iteration 7: [carboh, cart, carbu, cara, cari, carboni, carti, carbon, carr, carto, car, carbona] 

try it. tested small implementation. homework not think should give code directly ;) if stuck, post comment.

pseudocode

// step 0: initialize input , output  input <- [carbohydrate, cart, carburetor, caramel, caribou, carbonic, cartilage, carbon, carriage, carton, car, carbonate] output <- [ , , , , , , , , , , , ]  allfound <- false; // helper variable know if prefixes valid  while (not allfound) { // step 2 iteration: until prefixes ok      // simplicity, first count occurrences of each prefix     each prefix in output {         prefixcount[prefix] <- prefixcount[prefix] + 1 // may use map      }      // step 1: each prefix check if valid, if not add 1 more char     allfound <- true;      (i = 0; < output size; i++) {         if not (prefixcount[prefix] == 1 /* unique */ or output[i] equals input[i]) {             // prefix not ok                             output[i] <- output[i] + next char form input[i]; // add 1 more char             allfound <- false; // step 2 iteration condition "some prefix not valid"         }            }    }    

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -