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

该博客介绍了一种Java实现的高效词典搜索算法,用于查找输入字符串中所有可能的单词组合。通过加载词典文件并使用HashSet进行快速查找,程序可以在短时间内找到所有匹配的单词组合。示例展示了如何处理不同长度的输入,并输出所有找到的组合及其数量,以及搜索过程所花费的时间。
摘要由CSDN通过智能技术生成

小编典典

如果要支持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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值