当前位置: 首页 > article >正文

java 获取词典单词_Java词典搜索器

小编典典

如果要支持20个或更多的字符,以各种可能的方式拆分输入字符串将不会在合理的时间内完成。内联评论:这是一种更有效的方法:

public static void main(String[] args) throws IOException {

// load the dictionary into a set for fast lookups

Set dictionary = new HashSet();

Scanner filescan = new Scanner(new File("dictionary.txt"));

while (filescan.hasNext()) {

dictionary.add(filescan.nextLine().toLowerCase());

}

// scan for input

Scanner scan = new Scanner(System.in);

System.out.print("Enter a string: ");

String input = scan.next().toLowerCase();

System.out.println();

// place to store list of results, each result is a list of strings

List> results = new ArrayList<>();

long time = System.currentTimeMillis();

// start the search, pass empty stack to represent words found so far

search(input, dictionary, new Stack(), results);

time = System.currentTimeMillis() - time;

// list the results found

for (List result : results) {

for (String word : result) {

System.out.print(word + " ");

}

System.out.println("(" + result.size() + " words)");

}

System.out.println();

System.out.println("Took " + time + "ms");

}

public static void search(String input, Set dictionary,

Stack words, List> results) {

for (int i = 0; i < input.length(); i++) {

// take the first i characters of the input and see if it is a word

String substring = input.substring(0, i + 1);

if (dictionary.contains(substring)) {

// the beginning of the input matches a word, store on stack

words.push(substring);

if (i == input.length() - 1) {

// there's no input left, copy the words stack to results

results.add(new ArrayList(words));

} else {

// there's more input left, search the remaining part

search(input.substring(i + 1), dictionary, words, results);

}

// pop the matched word back off so we can move onto the next i

words.pop();

}

}

}

输出示例:

Enter a string: aman

a man (2 words)

am an (2 words)

Took 0ms

这是更长的输入:

Enter a string: thequickbrownfoxjumpedoverthelazydog

the quick brown fox jump ed over the lazy dog (10 words)

the quick brown fox jump ed overt he lazy dog (10 words)

the quick brown fox jumped over the lazy dog (9 words)

the quick brown fox jumped overt he lazy dog (9 words)

Took 1ms

2020-10-09

http://www.lryc.cn/news/2418035.html

相关文章:

  • 桌面运维之防火墙添加业务禁止访问的地址(持续更新中)
  • [硬件]导热垫(Thermal Pad)和导热过孔(Via for thermal pad)
  • 寒假来了,孩子学英语必看的8部动画!(内附资源)
  • django 异常分类
  • 虚拟机安装Debian 12.5.0及其常用软件(2024.7)
  • Windows2000高级技巧
  • UPS电源的常见故障维修方法
  • Java Session 会话技术
  • Linux搭建Hyperledger Fabric区块链框架 - Hyperledger Fabric模型概念
  • 上网行为管理系统推荐(这六款上网行为管理软件值得收藏)
  • Superset【部署 01】在线安装数据可视化图表工具 Superset(Python虚拟环境部署+问题解决+WEB登录配置+官方图表展示)
  • 《挪威的森林》
  • VoIP协议分析
  • 解释一下HTTP/2中的服务器推送(Server Push)机制及其优势。
  • C语言详解 FILE文件操作
  • 【ChatGPT实践】联网插件以及常见问题处理方案
  • 世界编辑器WorldEditor 1.2版本发布,八大功能强化易用性和自动化效率【文末有彩蛋】
  • Linux下的SVN服务器搭建
  • FLV封装格式解析
  • WMI Provider Host(wmiprvse.exe)占用CPU高的解决方案
  • 【Go语言入门教程】Go语言简介
  • 有趣的网站分享——pornhub风格生成器
  • Android系统广播大全
  • 如何使用阿里云GPU云服务器进行深度学习训练?
  • 03 外贸各国有效黄页搜索渠道【重要渠道资源】
  • BIOS 设置详解
  • 夜色空间轻装影院震撼发布,开启影音定制新纪元
  • 1024分辨率《变形金刚3》BD中英双字 高清 1080P 9G   720P 6G 下载
  • yoyo跑_yoyo主持人5岁女儿照片曝光 其老公魏哲浩个人资料简介
  • iphone、ipad备份还原ios降级SHSH篇,转载