trie + 长度优先匹配,生成串
import com.google.common.collect.Maps; import java.util.Map; /** * tree 节点 * Created by shuly on 16-7-18. */ public class Node { boolean isRoot; boolean isEnd; int cnt; Map<Character,Node> childrens; public Node(){ if (childrens == null) childrens = Maps.newHashMap(); this.cnt = 0; } public Node(boolean _isRoot,boolean _isEnd){ if (childrens == null) childrens = Maps.newHashMap(); setEnd(_isEnd); setRoot(_isRoot); this.cnt = 0; } public boolean isRoot() { return isRoot; } public void setRoot(boolean root) { isRoot = root; } public boolean isEnd() { return isEnd; } public void setEnd(boolean end) { isEnd = end; } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public Map<Character, Node> getChildrens() { return childrens; } public void setChildrens(Map<Character, Node> childrens) { this.childrens = childrens; } }
import com.google.common.collect.Lists; import java.util.Stack; import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * 树的主类 * Created by shuly on 16-7-18. */ public class PyTreeIT { Node ROOT; public PyTreeIT() { ROOT = new Node(true,false); numberLimit = new Integer(20); URL url = PyTreeIT.class.getClassLoader().getResource("pyDic"); String dicFilePath = url.getPath(); File dicFile = new File(dicFilePath); BufferedReader br = null ; try { String line; br = new BufferedReader(new InputStreamReader(new FileInputStream(dicFile), "UTF-8")); while((line = br.readLine()) != null) { String word = line.trim(); this.insert(word); } } catch (IOException e) { e.printStackTrace(); } finally { if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void insert(String word){ Node root= ROOT; char [] words = word.toCharArray(); for(int i = 0 ; i< words.length ; ++i){ Character key = words[i]; if(!root.getChildrens().containsKey(key)){ root.getChildrens().put(key,new Node(false,false)); } root = root.getChildrens().get(key); root.setCnt(root.getCnt()+1); } root.setEnd(true); } public int had(String word){ Node root = ROOT; char[] words = word.toCharArray(); for(int i = 0 ; i< words.length ; ++i){ Character key = words[i]; if(root.getChildrens().containsKey(key)){ root = root.getChildrens().get(key); }else return -1; } return root.isEnd()? root.getCnt(): 0; } static class InStack{ public String key,left; public InStack(String _key,String _left){ this.key = _key; this.left = _left; } @Override public String toString(){ return key +"--" + left; } } protected Integer numberLimit; private List<List<String>> dfs(String word) { if(word != null && word.equals("")){ return new ArrayList<List<String>>(); } List<List<String>> ans = Lists.newArrayList(); Stack<InStack> stack = new Stack<InStack>(); int pos = 0; stack.clear(); Node root = ROOT; while( pos < word.length() && root.getChildrens().containsKey(word.charAt(pos))) { root = root.getChildrens().get(word.charAt(pos)); if (root.isEnd()) { stack.push(new InStack(word.substring(0, pos + 1), word.substring(pos + 1))); } if (root.getCnt() == 1) { break; } ++ pos ; } while(!stack.empty()){ InStack now = stack.pop(); //末尾 if(now.left.equals("")){ List<String> inList = Lists.newArrayList(); inList.add(now.key); ans.add(inList); if(ans.size() == numberLimit){ return ans; } continue; } //非末尾 List<List<String>> leftStringList = dfs(now.left); for(List<String> item : leftStringList){ List<String> inList = Lists.newArrayList(); inList.add(now.key); inList.addAll(item); ans.add(inList); if(ans.size() == numberLimit){ return ans; } } } return ans; } public List<List<String>> pySplit(String word,Integer number){ numberLimit = number==null? 20:number; if(word.length() >= 60){ return new ArrayList< List<String> >(); } else return dfs(word); } public static void main(String[] args){ String it = "xiangangtiananmen"; PyTreeIT pyTree = new PyTreeIT(); List<List<String>>ans = pyTree.pySplit(it,20); if(ans == null){ System.out.println("TOT"); } else { for (List<String> item : ans) { for (String key : item) { System.out.print(key); System.out.print(" "); } System.out.println(""); } } System.out.println("over"); } }