我正在尝试编写一个程序,当给出两个字符串时,它“唠叨”一个字母来形成同义词。这是一个展示它的例子的网站:
http://www.braingle.com/brainteasers/46611/letter-juggle.html
我的任务是“编写一个程序 - 给定一个包含同义词对的文件 以及包含字典中单词序列的第二个文件 - 将尽可能多地从字典中产生一对单词,可用于为每个同义词对设置拼图。“
这些是文件 - dictionary.txt和synonyms.txt。
当我耍弄一个单词时,我会查看字典以查看它是否有效。所以说“吹嘘”和“嘻哈”,当我把它们搞砸时,我可以得到“船”和“船”(这是同义词)。
现在,我已经拿了两根琴弦(钉子和别针)并将它们拆分成一个char数组,但我不知道如何处理它们以检查它们是否是有效的单词。
我希望能够将“钉子”中的字母“n”添加到“pin”以给我“pinn”,我想要 然后通过“pinn”的每个组合并检查它是否是一个有效的单词 - 如果是,我然后检查“ail”是否可以是一个单词,如果没有,那么我继续下一个字母“钉子” pinn - > pinn,pnin,pnni,pnin ......
public class LetterJuggle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Dictionary.txt"); //Dictionary.txt //Synonyms.txt
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int size =0;
while ((strLine = br.readLine()) != null){
size++;
}
String [] dictionary = new String [size];
fstream = new FileInputStream("Dictionary.txt");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
size = 0;
//Read File Line By Line
while ((strLine = br.readLine()) != null){
// Print the content on the console
dictionary[size] = strLine;
size++;
}
fstream = new FileInputStream("Synonyms.txt");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null){
//System.out.println(strLine);
String [] words = strLine.split("\\s+");
for(int i =0; i < words.length; i++){
//System.out.println(words[i]);
}
char[] ch_array_1 = words[0].toCharArray();
char[] ch_array_2 = words[1].toCharArray();
for(int i =0; i < ch_array_1.length; i++){
System.out.print(ch_array_1[i] + " ");
}
System.out.println();
for(int i =0; i < ch_array_2.length; i++){
System.out.print(ch_array_2[i] + " ");
}
System.out.println();
}
//Close the input stream
in.close();
}catch(Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
答案 0 :(得分:2)
它不一定有效,但这是一个想法。使用一些(2)循环并从第一个单词中取一个字母并将其添加到所有位置(从索引0到最后一个字母索引之后)并检查两者是否都是有效单词(带有删除字母的单词和新形成的单词)。 (一些伪代码)
for (Letter l : word1)
{
Word word1temp = extract_Letter_l_from_word(l,word1);
check if word1temp and word2 are synonyms
//else
for (all letter indexes i of word2)
{
form word with letter L at position i and word2 form a synonim of word2
// also maybe do this in the mirror for word2 and word1
}
}
答案 1 :(得分:1)
试试这个,数组的排列:Permutation of Array
在此处发布代码,以便即使链接已过时,您也可以参考此处。我认为这有助于你
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.lang.reflect.Array;public class Permute implements Iterator {
private final int size;
private final Object [] elements; // copy of original 0 private final Object ar; // array for output, private final int [] permutation; // perm of nums 1..siprivate boolean next = true;
// int[], double[] array won't work :-(
public Permute (Object [] e) {
size = e.length;
elements = new Object [size]; // not suitable for System.arraycopy (e, 0, elements, 0, size);
ar = Array.newInstance (e.getClass().getComponentType System.arraycopy (e, 0, ar, 0, size);
permutation = new int [size+1];
for (int i=0; i permutation [i]=i;
}
}private void formNextPermutation () {
for (int i=0; i // i+1 because perm[0] always = 0
// perm[]-1 because the numbers 1..size are being Array.set (ar, i, elements[permutation[i+1]-1]);
}
}public boolean hasNext() {
return next;
}public void remove() throws UnsupportedOperationExceptio throw new UnsupportedOperationException();
}private void swap (final int i, final int j) {
final int x = permutation[i];
permutation[i] = permutation [j];
permutation[j] = x;
}// does not throw NoSuchElement; it wraps around!
public Object next() throws NoSuchElementException {formNextPermutation (); // copy original elements int i = size-1; while (permutation[i]>permutation[i+1]) i--; if (i==0) { next = false; for (int j=0; j<size+1; j++) { permutation [j]=j; } return ar; } int j = size; while (permutation[i]>permutation[j]) j--; swap (i,j); int r = size; int s = i+1; while (r>s) { swap(r,s); r--; s++; } return ar;
}
public String toString () { final int n = Array.getLength(ar); final StringBuffer sb = new StringBuffer ("["); for (int j=0; j<n; j++) { sb.append (Array.get(ar,j).toString()); if (j<n-1) sb.append (","); } sb.append("]"); return new String (sb);
}
public static void main (String [] args) {
for (Iterator i = new Permute(args); i.hasNext(); ) { final String [] a = (String []) i.next();
System.out.println (i);
}
}
}