2012-12-18 47 views
-1

我試圖寫一個程序,給定兩個字符串時,「種子隊」的一封信周圍形成同義詞。這裏有一個網站,顯示它的一個例子:的字符串組合/排列

http://www.braingle.com/brainteasers/46611/letter-juggle.html

我的任務是「寫一個程序 - 鑑於含有對同義詞 和字典包含的單詞序列的第二個文件的文件 - 將會從字典中產生儘可能多的單詞,這些單詞可以用來爲每個同義詞對設置拼圖。「

這些文件 - dictionary.txtsynonyms.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()); 
    }  


} 

}

+1

@PradeepSimha所有我到目前爲止是兩個字符串的字符數組。我只是尋找一種很好的方式來處理這些信件並檢查它們是否有效。我不一定要求代碼。 –

+0

你的意思是陣列旋轉? –

+0

請在這裏顯示你的程序,然後我們可以很容易地幫你 – Abhishekkumar

回答

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

我相信可能有更高效/更聰明的方法來解決這個問題,但這是第一個想到的第一個想法 – acostache

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..si

private 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);        

}

公共靜態無效的主要(字串[] args){
的(迭代器I =新的置換(參數); i.hasNext();){ final String [] a =(String [])i.next();
System.out.println(i);
}
}
}

+1

請原諒我的代碼格式 –