我試圖寫一個程序,給定兩個字符串時,「種子隊」的一封信周圍形成同義詞。這裏有一個網站,顯示它的一個例子:的字符串組合/排列
http://www.braingle.com/brainteasers/46611/letter-juggle.html
我的任務是「寫一個程序 - 鑑於含有對同義詞 和字典包含的單詞序列的第二個文件的文件 - 將會從字典中產生儘可能多的單詞,這些單詞可以用來爲每個同義詞對設置拼圖。「
這些文件 - dictionary.txt和synonyms.txt。
當我玩弄了一個字,我查了字典,看它是否是有效的。所以,當我把「誇耀」和「臀部」這兩個詞放在一起時,我就可以得到「船」和「船」(它們是同義詞)。
現在,我已經採取了兩個字符串(指甲和腳),並將它們分割成一個字符數組,但我不知道如何兼顧他們檢查,如果它們是有效的話。
我希望能夠將「釘子」中的字母「n」添加到「pin」中給我「pinn」,我想 然後通過「pinn」的每個組合並檢查它是否有效單詞 - 如果是,我然後檢查是否「所有」可以是一個單詞,如果不是的話,那麼我繼續看下一個字母「釘子」 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());
}
}
}
@PradeepSimha所有我到目前爲止是兩個字符串的字符數組。我只是尋找一種很好的方式來處理這些信件並檢查它們是否有效。我不一定要求代碼。 –
你的意思是陣列旋轉? –
請在這裏顯示你的程序,然後我們可以很容易地幫你 – Abhishekkumar