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 + 长度优先匹配,生成串的更多相关文章

  1. 【EF 3】浅谈ADO数据模型生成串(一):csdl,ssdl,msl分析

    导读:这段经历,真的是难以忘怀.恨得我牙痒痒,就一个字符串拼接,前前后后尽然报了不下30个错.有的错很快就能调出来,有的错调一天.两天,是真的可以的.最终总结了一下,这些错很大一部分原因是对于EF生成 ...

  2. 【EF 2】浅谈ADO数据模型生成串(二):数据库连接串分析

    导读:上篇博客中介绍了ADO生成串的前一部分,本篇博客结合报错,接着介绍剩下的部分. 一.代码展示 <span style="font-family:KaiTi_GB2312;font ...

  3. hiho#1449 重复旋律6 求长度为k的串最大次数 后缀自动机

    题目传送门 题目大意:求长度为k的串的最大次数,把k从1到length的所有答案全部输出. 思路: 这道题放在$SAM$里就是求长度$k$对应的所有$right$集中最大的大小. 我们以$aabab$ ...

  4. HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同

    n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...

  5. 用正则匹配一串字符串中的ip地址

    IP地址有4段组成,每一段数字的范围为0-255,在一段文本中提取ip地址可以这样 $src = 'src = alsdlk ks sdf2.3.3.4 234.193.1.120.1232 d.23 ...

  6. HDU 1711 Number Sequence(KMP匹配数字串)

    这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...

  7. php 分词 —— PHPAnalysis无组件分词系统

    分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...

  8. 一个很好的php分词类库

    PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档   原文连接地址:http://www.phpbone.co ...

  9. php 分词

    发现了一个很好的分词类库phpanalysis2.0. 原文连接地址:http://www.phpbone.com/phpanalysis/         分 词系统简介:PHPAnalysis分词 ...

随机推荐

  1. 辛星PHP教程之yii和ci教程已经写完,望与朋友们交流

     记得有个朋友给我说,你写的PHP框架是不是过于少了.我感觉仅仅有一个thinkphp确实不好,于是就又写了下yii和ci的教程,事实上我之前是研究过这两个框架的,因此写起来也还算得心应手吧.估计接下 ...

  2. Access Violation at address 00000000.Read of address 00000000 解决办法

    是数组越标或没有初始化某个对象之类的问题,搂住细细检查一下代码, 使用指针前未做检查,而这个指针未初始化. 可能是new后没有delete,这样出现溢出的可能性比较大     检查代码或者跟踪试试 使 ...

  3. 在qt中用tcp传输xml消息

    在qt中用tcp传输xml消息 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上 ...

  4. NET实现的DDD、CQRS与微服务架构

    WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间 ...

  5. winXP JDK由1.8改为1.6

    (1)直接在环境变量中删除配置的相关路径 path的值: C:\Documents and Settings\Administrator>path PATH=C:\Documents and S ...

  6. CSDN 四川大学线下编程比赛第二题:Peter的X

    题目详情: http://student.csdn.net/mcs/programming_challenges Peter是个小男孩.他总是背不清26个英文字母. 于是,刁钻的英语老师给他布置了一个 ...

  7. javascript它【蛇系列】第一弹:简单的蛇实现

    参考博客:http://blog.csdn.net/sunxing007/article/details/4187038 上面的博客是一个参考,竟第一次做.真让自己盲人摸象做不出来. 只是我在其上做了 ...

  8. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...

  9. poj3264(线段树区间求最值)

    题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...

  10. NGUI研究之在Unity中使用贝塞尔曲线

    鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天由于工作的原因须要将贝塞尔曲线加在project中.那么我迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的随意角度的曲线,这两个点一 ...