-1

私は、2つの文字列が与えられると、文字を「ジャグリング」して同義語を形成するプログラムを作成しようとしています。その例を示すWebサイトは次のとおりです。

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

私の仕事は、「同義語のペアを含むファイルと、辞書からの単語のシーケンスを含む2番目のファイルを指定して、パズルを設定するために使用できる辞書からできるだけ多くの単語のペアを生成するプログラムを作成することです。同義語のペアごとに。」

これらはファイルです-dictionary.txtsynonyms.txt

単語をジャグリングするときは、辞書をチェックして、それが有効かどうかを確認します。つまり、「自慢」と「ヒップ」という言葉を使って、それらをジャグリングすると、「ボート」と「船」(同義語)を取得できます。

今、私は2つの文字列(ネイルとピン)を取り、それらをchar配列に分割しましたが、それらが有効な単語であるかどうかを確認するためにそれらをジャグリングする方法がわかりません。

「nail」から「pin」に文字「n」を追加して「pinn」を取得できるようにしたいので、「pinn」のすべての組み合わせを調べて、それが有効な単語であるかどうかを確認します。 、次に、「ail」が単語になり得るかどうかを確認します。そうでない場合は、「nail」の次の文字に移動します。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());
    }    


}

}

4

2 に答える 2

2

それは必ずしも効率的ではありませんが、アイデアです。いくつかの (2) ループを使用して、最初の単語から 1 つの文字を取得し、それをすべての位置 (インデックス 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
    }
}
于 2012-12-18T15:05:20.717 に答える
1

これを試してください、配列の順列:配列の順列

ここにコードを投稿したため、リンクも古くなっています。ここを参照できます。私はこれがあなたを助けると思います

import java.util.Iterator;
java.util.NoSuchElementException をインポートします。
import java.lang.reflect.Array;

public class Permute は Iterator を実装します {

プライベートの最終的な int サイズ。
プライベート最終オブジェクト [] 要素。// 元の 0 のコピー プライベート最終オブジェクト ar; // 出力用の配列、 private final int [] 順列; // 数値 1..si のパーマ

プライベート ブール次 = true;

// int[]、double[] 配列は機能しません :-(
public Permute (Object [] e) {
size = e.length;
要素 = 新しいオブジェクト [サイズ]; // には適していません System.arraycopy (e、0、要素、0、サイズ);
ar = Array.newInstance (e.getClass().getComponentType System.arraycopy (e、0、ar、0、サイズ);
順列 = 新しい int [サイズ + 1];
for (int i=0; i 順列[i]=i;
}
}

プライベート ボイド formNextPermutation () {
for (int i=0; i // perm[0] は常に = 0 なので i+1
// perm[]-1 というのは、数字 1..size が処理されているからです。 Array.set (ar, i, elements[permutation[i+1]-1]);
}
}

public boolean hasNext() {
return next;
}

public void remove() は UnsupportedOperationExceptionio をスローします 新しい UnsupportedOperationException() をスローします。
}

private void swap (final int i, final int j) {
final int x = permutation[i];
順列[i] =順列[j];
順列[j] = x;
}

// NoSuchElement をスローしません。包み込む!
public Object next() は 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(); ) { 最終文字列 [] a = (文字列 []) i.next();
System.out.println (i);
}
}
}

于 2012-12-18T15:17:34.630 に答える