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");
}
}
trie + 长度优先匹配,生成串的更多相关文章
- 【EF 3】浅谈ADO数据模型生成串(一):csdl,ssdl,msl分析
导读:这段经历,真的是难以忘怀.恨得我牙痒痒,就一个字符串拼接,前前后后尽然报了不下30个错.有的错很快就能调出来,有的错调一天.两天,是真的可以的.最终总结了一下,这些错很大一部分原因是对于EF生成 ...
- 【EF 2】浅谈ADO数据模型生成串(二):数据库连接串分析
导读:上篇博客中介绍了ADO生成串的前一部分,本篇博客结合报错,接着介绍剩下的部分. 一.代码展示 <span style="font-family:KaiTi_GB2312;font ...
- hiho#1449 重复旋律6 求长度为k的串最大次数 后缀自动机
题目传送门 题目大意:求长度为k的串的最大次数,把k从1到length的所有答案全部输出. 思路: 这道题放在$SAM$里就是求长度$k$对应的所有$right$集中最大的大小. 我们以$aabab$ ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- 用正则匹配一串字符串中的ip地址
IP地址有4段组成,每一段数字的范围为0-255,在一段文本中提取ip地址可以这样 $src = 'src = alsdlk ks sdf2.3.3.4 234.193.1.120.1232 d.23 ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
- php 分词 —— PHPAnalysis无组件分词系统
分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...
- 一个很好的php分词类库
PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档 原文连接地址:http://www.phpbone.co ...
- php 分词
发现了一个很好的分词类库phpanalysis2.0. 原文连接地址:http://www.phpbone.com/phpanalysis/ 分 词系统简介:PHPAnalysis分词 ...
随机推荐
- ThinkPHP框架配置自定义的模板变量(十)
原文:ThinkPHP框架配置自定义的模板变量(十) 模板替换(手册有详细介绍对应的目录) __PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/ __ROOT__: 会替换成当 ...
- 积累的VC编程小技巧之组合框
1.如何正确的得到ComBox的指针 CComboBox *mComb = (CComboBox*)GetDlgItem(IDC_DuanCB); CComboBox *mComb = (CCombo ...
- How to convert `ctime` to `datetime` in Python? - Stack Overflow
How to convert `ctime` to `datetime` in Python? - Stack Overflow How to convert `ctime` to `datetime ...
- 全面总结:matlab怎么做漂亮的图
源地址:http://blog.csdn.net/ccxcau/article/details/7362764 MATLAB受到控制界广泛接受的一个重要原因是因为它提供了方便的绘图功能.本章主要介绍2 ...
- 浏览器url传参中文时得到null的解决方法
在写一个中文参数需求的时候遇到了以下问题,经过半天的测试和各种编码,以及网上一些有用没用的资料尝试终于解决 比如下面的url地址:http://travel.widget.baike.com:8 ...
- poj-2195-Going Home最小费用最大流
重新切一遍最小费用最大流~~~ 这到题目的数据范围有问题,尽量开大就好了~~ #include<stdio.h> #include<iostream> #include< ...
- 西南民大oj(递推)
我的数学不可能那么难推 时间限制(普通/Java) : 3000 MS/ 9000 MS 运行内存限制 : 65536 KByte总提交 : 49 测试通过 : ...
- python六核心编程——条件和循环
1.if声明 单 if 通过使用布尔运算符的声明 and , or 和 not. if-elif-else. elif即else if if expression1: expr1_true_ ...
- SPOJ 375(树链剖分)
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 题意:一棵包含N 个结点的树,每条边都有一个权值, ...
- Android它SDK Manager无法更新终极解决方案
前些日子.Google终于发布Android5.0正式版--棒糖.也许你和我一样,,此外,我想在第一时间更新SDK,结果打开SDK Manager,但令人失望,络围墙啊.默默问一句:近期好久没有听到方 ...