java - 递归将一个单词分成三个或更多单词java
问题描述
我正在尝试编写一个程序,该程序将从 dictionary.txt 文档中获取输入并将单词存储在字典列表中,然后确定该单词是否可以分解为三个或更多单词,如果可以,则打印出原始单词例如,后跟新单词的单词disconsolateness:disc on so lateness
将是文档composedMore.txt 中的输出。现在,代码继续运行,但我没有得到任何输出,我不确定我做错了什么。您能提供的任何帮助将不胜感激。我在下面发布了我的代码,输入是字典中的任何单词。
import java.util.*;
import java.io.*;
public class CompositionTwo
{
private static List<String> dictionary = new ArrayList<>();
public static void main(String []args) {
File inputFile = new File("dictionary.txt");
File outputFile = new File("composedMore.txt");
Scanner in = null;
PrintWriter out = null;
try {
in = new Scanner(inputFile);
out = new PrintWriter(outputFile);
while (in.hasNext()) {
String input = in.next();
dictionary.add(input);
String output = splitWord(input, "");
if (output != "") {
out.println(input + ":" + output);
}
}
} catch (IOException e) {
System.out.println("An IOException has occurred during output process.");
} finally {
in.close();
out.close();
}
}
public static String splitWord(String word, String output) {
if (word.length() == 0) {
output = output;
}
else {
for (int i = 1; i <= word.length(); i++) {
// consider all prefixes of current String
String prefix = word.substring(0, i);
// if the prefix is present in the dictionary, add prefix to the
// output String and recurse for remaining String
if (dictionary.contains(prefix)) {
splitWord(word.substring(i), output + " " + prefix);
}
}
}
return output;
}
}
解决方案
首先将所有单词添加到字典中,然后检查每个单词,因为您需要与文件中的所有单词进行比较您采用的过程只是比较了被比较单词之前的单词
import java.util.*;
import java.io.*;
public class CompositionTwo
{
private static List<String> dictionary = new ArrayList<>();
public static void main(String []args) {
File inputFile = new File("dictionary.txt");
File outputFile = new File("composedMore.txt");
Scanner in = null;
PrintWriter out = null;
String word;
try {
in = new Scanner(inputFile);
out = new PrintWriter(outputFile);
//Read the file indictionary
while (in.hasNext()) {
String input = in.next();
dictionary.add(input);
}
//Check All the words in dictionary for Splitting
for(int i=0;i<dictionary.size();i++)
{
String output = splitWord(dictionary.get(i), "");
if (!"".equals(output)) {
String outa[] = output.split("\\s") ;
if(outa.length >= 3) // Check if 3 words are created as output
{
System.out.println(dictionary.get(i) + ":" + output);
out.println(dictionary.get(i) + ":" + output);
}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
in.close();
out.close();
}
}
public static String splitWord(String word, String output) {
if (word.length() == 0) {
return output;
}
else {
for (int i = 1; i <= word.length(); i++) {
// consider all prefixes of current String
String prefix = word.substring(0, i);
// if the prefix is present in the dictionary, add prefix to the
// output String and recurse for remaining String
if (dictionary.contains(prefix)) {
return splitWord(word.substring(i), output + " " + prefix);
}
}
}
return output ;
}
}
推荐阅读
- javascript - Parsley.js:如何制作一个现有验证器的自定义验证器
- java - 矩阵商店。我们要去哪里?
- python - 如何获得给定 2 个数组的随机样本?
- linux - 简单备份脚本的进度条
- shell - 从 ansible 运行 sed 命令
- laravel - 为什么可以从所有命名空间访问 laravel 助手?
- html - CSS 网格属性:创建响应式全屏“纵向”图像网格,保留 767 及以上屏幕宽度的图像纵横比
- c++ - 虽然运行了一段时间,但程序已经停止工作?
- python - 在 Python 诅咒中显示扩展的 Unicode 字符
- matlab - 正则化逻辑回归计算代价函数和梯度面临的问题