Java实现双数组Trie树(DoubleArrayTrie,DAT) (四)

2014-11-24 08:07:36 · 作者: · 浏览: 6
int toBeAdjust;
ArrayList list = null;
if(true)
{
toBeAdjust = pre_p;
list = list1;
}

int origin_base = Base[toBeAdjust];
list.add(GetCharCode(s.charAt(i)));
int avail_base = x_check((Integer[])list.toArray(new Integer[list.size()]));
list.remove(list.size()-1);

Base[toBeAdjust] = avail_base;
for(int j=0; j {
//BUG
int tmp1 = origin_base + list.get(j);
int tmp2 = avail_base + list.get(j);

Base[tmp2] = Base[tmp1];
Check[tmp2] = Check[tmp1];

//有后续
if(Base[tmp1] > 0)
{
ArrayList subsequence = GetChildList(tmp1);
for(int k=0; k {
Check[Base[tmp1]+subsequence.get(k)] = tmp2;
}
}

Base[tmp1] = 0;
Check[tmp1] = 0;
}

//更新新的cur_p
cur_p = Base[pre_p]+GetCharCode(s.charAt(i));

if(s.charAt(i) == END_CHAR)
Base[cur_p] = 0;
else
Base[cur_p] = -Pos;
Check[cur_p] = pre_p;
Pos = CopyToTailArray(s,i+1);
break;
}
}
}

public boolean Exists(String word)
{
int pre_p = 1;
int cur_p = 0;

for(int i=0;i {
cur_p = Base[pre_p]+GetCharCode(word.charAt(i));
if(Check[cur_p] != pre_p) return false;
if(Base[cur_p] < 0)
{
if(TailMatchString(-Base[cur_p],word.substring(i+1)))
return true;
return false;
}
pre_p = cur_p;
}
if(Check[Base[cur_p]+GetCharCode(END_CHAR)] == cur_p)
return true;
return false;
}

//内部函数,返回匹配单词的最靠后的Base index,
class FindStruct
{
int p;
String prefix="";
}
private FindStruct Find(String word)
{
int pre_p = 1;
int cur_p = 0;
FindStruct fs = new FindStruct();
for(int i=0;i {
// BUG
fs.prefix += word.charAt(i);
cur_p = Base[pre_p]+GetCharCode(word.charAt(i));
if(Check[cur_p] != pre_p)
{
fs.p = -1;
return fs;
}
if(Base[cur_p] < 0)
{
if(TailContainString(-Base[cur_p],word.substring(i+1)))
{
fs.p = cur_p;
return fs;
}
fs.p = -1;
return fs;
}
pre_p = cur_p;
}
fs.p = cur_p;
return fs;
}

public ArrayList GetAllChildWord(in