gpt4 book ai didi

java - 使用尝试自动完成

转载 作者:行者123 更新时间:2023-12-01 13:12:58 25 4
gpt4 key购买 nike

import java.util.ArrayList;


public class AutoComplete {
boolean newWord = false;
ArrayList<String> all = new ArrayList<String>();

static TrieNode root = new TrieNode('!', false, null);

void add(String s){
TrieNode temp = root;
// TrieNode parent = root;

for(int i=0; i < s.length(); i++){
int t = s.charAt(i);
t = t - 97;
while((temp.links[t]) != null && i < s.length()-1){

temp = temp.links[t];
t = s.charAt(++i);
t = t - 97;

// parent = temp;
}
if( i != s.length()-1){
temp.links[t] = new TrieNode((char)(97+t), false, null);
// parent = temp.links[t];
}
else{
temp.links[t] = new TrieNode((char) (97+t), true, null);
// parent = temp.links[t];
}
temp = temp.links[t];
}
}

void readTree(String find){
int len = find.length();
int i = 0;
TrieNode temp = root;
String match = "";
while(i != len){
int t = find.charAt(i);
t = t - 97;
temp = temp.links[t];
if(temp == null)
break;
match = match + temp.letter;
i++;
}
if(match.length() > 0)
match = match.substring(0,match.length()-1);
printAll(temp, match);
}

void printAll(TrieNode t, String parent){
if(t== null)
return;
parent = parent + t.letter;
if(t.fullWord){
System.out.println(parent);
}
for(int i = 0; i < 26; i++){
printAll(t.links[i], parent);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
AutoComplete a = new AutoComplete();

a.add("tea");
a.add("team");
a.add("teach");
a.add("teacher");
a.readTree("t");
}

}

我正在尝试使用 trie 实现自动完成,当 trie 中的元素从较低长度添加到较高长度时它工作正常

如果我按此顺序添加元素

a.add("tea");
a.add("team");
a.add("teach");
a.add("teacher");

我得到以下输出 a.readTree("t");

tea
teach
teacher
team

但是如果我按这个顺序添加元素

a.add("teacher");
a.add("teach");
a.add("team");
a.add("tea");

我得到以下输出 a.readTree("t");

tea

最终工作解决方案

public class AutoComplete {
void add(String s, TrieNode root){
//To keep the root node intact
TrieNode temp = root;

//Iterate through each char in string s
for(int i=0; i < s.length(); i++){
//each character in string
int t = s.charAt(i);
//its corresponding value in array links
t = t - 'a';
//if some part of string is already present in array then just loop through it except th
while((temp.links[t]) != null && i < s.length()-1){
//increment i since first char is present
i = i +1;
//increment in trie
temp = temp.links[t];
//go to next char in string and
//go to that char location in array
t = s.charAt(i)- 'a';
}
//Add only till before the last character
if( i < s.length()-1){
temp.links[t] = new TrieNode((char)('a'+t), false);
}
//for last character of string
else{
// if last character is not present
if(temp.links[t] == null){
temp.links[t] = new TrieNode((char) ('a'+t), true);
}
// if last character already exist
else{
temp.links[t].fullWord = true;
}
}
//increment the trie
temp = temp.links[t];
}
}
void readTree(String find, TrieNode root){
//get length in len
int len = find.length();
int i = 0;
TrieNode temp = root;
//initialize string to store the result
String match = "";
while(i < len){
//get first char of search string
int t = find.charAt(i) - 'a';
//go to its array location
temp = temp.links[t];
//location is null then break else continue
if(temp == null)
break;
//keep appending the found char and increment the index
match = match + temp.letter;
i++;
}
//if suggestions exist
if(match.length() > 0)
//pass the match string except for the last element
match = match.substring(0,match.length()-1);
printAll(temp, match);
}

void printAll(TrieNode t, String parent){
if(t== null)
return;
parent = parent + t.letter;
if(t.fullWord){
System.out.println(parent);
}
for(int i = 0; i < 26; i++){
printAll(t.links[i], parent);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
TrieNode root = new TrieNode('!', false);
AutoComplete a = new AutoComplete();

a.add("tea", root);
a.add("team", root);
a.add("teach", root);
a.add("teacher", root);
a.readTree("t", root);
}

}

最佳答案

问题出在这里:

else{ //i == s.length - 1
temp.links[t] = new TrieNode((char) (97+t), true, null);
}

当您添加“teach”时,它将在字符“h”处新一个节点,该节点替换“teacher”的节点“h”。你应该这样写:

else{ //i == s.length - 1
if(temp.links[t] == null) {
temp.links[t] = new TrieNode((char) (97+t), true, null);
}
else {
// change the leaf tag from false to true
}
}

关于java - 使用尝试自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com