首先要建立字符串列表
这里用树来实现,xxx率的优化再慢慢研究
不说了,上代码
mport java.util.regex.*;
public class WordTree {
private WordTreeNode rootNode;
public WordTree(){
}
public WordTree(String []wordList){
//很多地方都是测试用,wordList大小不能超过int上限
//这个还要控制,程序很多地方没有
for(int i = 0;i<wordList.length;i++){
this.insert(wordList[i]);
}
}
public WordTreeNode getRootNode(){
return this.rootNode;
}
public void setRootNode(WordTreeNode rootNode){
this.rootNode = rootNode;
}
public String []getWords(String s){
String []result = null;
WordTreeNode tempNode = null;
WordTreeNode [] tempNodeList;
int i = 0;
int arrayIndex;
if(this.getRootNode() == null){
return null;
}else{
tempNode = this.getRootNode();
while(i!=s.length()){
System.out.println(i);
tempNodeList = tempNode.getNodeList();
arrayIndex = Integer.parseInt(String.valueOf(s.charAt(i)));
System.out.println("arrayIndex="+arrayIndex);
for(int k = 0;k<tempNodeList.length;k++){
if(tempNodeList[k]!=null)
System.out.println(k+"非空");
}
if(tempNodeList[arrayIndex]==null){
System.out.println("这个是null");
return null;
}else{
System.out.println("tempnode change"+arrayIndex);
tempNode = tempNodeList[arrayIndex];
}
i++;
}
}
String wordList = tempNode.getWordList();
if(wordList == null) return null;
result = wordList.split(",");
return result;
}
public void insert(String s){
if( s == null) return;
if( s.length() == 0) return;
//没有这么长的英文单词
if( s.length() > 200) return;
String regExp = "[a-z|-]*";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(s);
if(!m.find()) return;
//rootNode为空,{dy}次创建
if(rootNode == null){
int i = 0;
int arrayIndex;
char temp;
WordTreeNode []tempNodeList;
WordTreeNode tempNode = new WordTreeNode();
//用来存放tempNode的子结点
WordTreeNode tempChildNode;
tempNode.setNodeLevel(1);
this.setRootNode(tempNode);
while(i != (s.length())){
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempNode = tempChildNode;
System.out.println("ggggg"+arrayIndex);
}else{
tempNode = tempNodeList[arrayIndex];
}
i++;
System.out.println("循环"+i+" "+arrayIndex);
}//处理{zh1}一个字符
temp = s.charAt(i-1);
arrayIndex = this.getNumber(temp);
//tempNodeList = tempNode.getNodeList();
System.out.println("root==null;处理{zh1}一个字符"+s.charAt(i-1)+arrayIndex);
tempNode.insert(s);
/**if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempChildNode.insert(s);
}else{
tempChildNode = tempNodeList[arrayIndex];
tempChildNode.insert(s);
}*/
//end处理{zh1}一个字符
}else{
int i = 0;
int arrayIndex;
char temp;
WordTreeNode []tempNodeList;
WordTreeNode tempNode = this.getRootNode();
//用来存放tempNode的子结点
WordTreeNode tempChildNode;
//tempNode.setNodeLevel(1);
//this.setRootNode(tempNode);
while(i != (s.length()-1)){
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempNode = tempChildNode;
}else{
tempNode = tempNodeList[arrayIndex];
}
i++;
}
System.out.println("处理{zh1}一个字符");
//处理{zh1}一个字符
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempChildNode.insert(s);
System.out.println("insert: "+s);
}else{
tempChildNode = tempNodeList[arrayIndex];
tempChildNode.insert(s);
System.out.println("insert: "+s);
}
//end
}
}
public int getNumber(char c){
int result = 0;
int temp = (int)c;
if(temp == 122){
result = 9;
}else if(temp ==45){
result = 0;
}else if(temp >=112){
switch (c){
case 'p':
case 'q':
case 'r':
case 's':
result = 7;
break;
case 't':
case 'u':
case 'v':
result = 8;
break;
case 'w':
case 'x':
case 'y':
case 'z':
result = 9;
break;
}
}
else{
result = (int)((temp-97)/3) + 2;
}
//System.out.println(c+"返回"+result);
return result;
}
}
public class WordTreeNode {
//这个nodeList定义为几个要看系统实现了
//一般来说,英语单词里面是有 - 连字符的
//除了2-9这8个数字以外,有个连字符可以增加容错性
private WordTreeNode []nodeList;
private int nodeLevel;
private String wordList;
public WordTreeNode(){
nodeList = new WordTreeNode[10];
for(int i = 0;i < nodeList.length;i++){
nodeList[i] = null;
}
wordList = null;
nodeLevel = 0;
}
public int getNodeLevel(){
return this.nodeLevel;
}
public void setNodeLevel(int nodeLevel){
this.nodeLevel = nodeLevel;
}
public WordTreeNode []getNodeList(){
return this.nodeList;
}
public void setNodeList(WordTreeNode []nodeList){
this.nodeList = nodeList;
}
public String getWordList(){
return this.wordList;
}
public void setWordList(String wordList){
this.wordList = wordList;
}
public void insert(String s){
if(this.getWordList() == null){
this.setWordList(s);
}else{
if(this.getWordList().indexOf(s)==-1){
this.setWordList(this.getWordList()+","+s);
}
}
}
}
测试用:
public class MainClass {
public static void main(String []args){
String []s={"my","yaowei","jdk","jdk","jek"};
WordTree w = new WordTree(s);
System.out.println(w.getWords("535")[1]);
}
}