【发布时间】:2014-11-27 08:19:50
【问题描述】:
这个算法的目标是获取一个字符串列表并以这样一种方式缩写它们,以便它们可以唯一地识别它们所代表的单词,这是一个家庭作业。
我尝试过的算法为这个问题创建搜索子字符串以寻找任何可能的匹配项,虽然看起来 trie 对这个解决方案更有效,但我似乎无法理解代码的工作原理;话虽如此,这样做也应该很有可能。
输入:~~~~~~~~~~~~ 预期输出:~~~~~~~~~~~~ 实际输出:
碳水化合物 -------- 碳水化合物 -------------------------------- 碳水化合物
购物车 -------------------- 购物车 ---------------------------- -------- 购物车
化油器 ------------ carbu --------------------------------- 化油器
焦糖 --------------- 卡拉 --------------------------------- -- 焦糖
驯鹿 --------------- cari --------------------------------- ---- 驯鹿
碳酸 -------------- 碳酸 -------------------------------- 碳酸
软骨 -------------- 软骨 ---------------------- -- 软骨
碳 ---------------- 碳 -------------------------------- - 碳
马车 -------------- 马车 -------------------------------- carr
纸箱 ----------------- 纸箱 -------------------------------------------- ---- 卡托
汽车 --------- 汽车 ---------------------------- ---------- 车
碳酸盐 ------------ 碳酸盐 ------------------- 碳酸盐
这是实际代码。
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());
for (int count = 0; count<input.size(); count++) //This loop counts which word is being reduced
{
//System.out.println(count);
String word = input.get(count);
//System.out.println(word);
for (int compare = 0; compare<input.size(); compare++)//this loop counts the characters of the word being reduced
{
if(input.get(compare) .equals(word)){}
isEqual=false;
ArrayList<String> wordBeingComparedRightNow = new ArrayList<>();
for (int searchCount = 0; searchCount<input.get(searchCount).length(); searchCount++)//this loop compares the string to every possible substring
{
wordBeingComparedRightNow.clear();
String wordAboutToBeCompared = input.get(searchCount);
wordBeingComparedRightNow.add(wordAboutToBeCompared);
for (int miniLoop = 0; miniLoop<input.get(searchCount).length()-1; miniLoop++)//this loop sets up for a word's substrings to be tested
{
wordAboutToBeCompared = wordAboutToBeCompared.substring(0, wordAboutToBeCompared.length() -1);
wordBeingComparedRightNow.add(wordAboutToBeCompared);
}
for(int variableName = 0; variableName < wordBeingComparedRightNow.size(); variableName++)//this word compares the array craeated in the last step with the substring we currently 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);
}
}
我认为问题出在第二个 for 循环中的某个地方,但我不太清楚在哪里;如果有人能确定我的算法为什么不起作用,或者至少能指出我正确的方向;我真的很感激。 谢谢。
【问题讨论】:
-
if(input.get(compare) .equals(word)){}的目的是什么? -
你如何得到这个映射
carbohydrate -------- carboh -------------------------------- carbohydrate输入中没有你期望的 carboh? -
if(input.get(compare) .equals(word){} 是我用来尝试通过强制循环继续()来调试我的代码的东西;但我在某些时候删除了它把它当作垃圾的点。