2017-02-14 40 views
0

我正在使用下面的代碼根據特定的表對字符串進行置換。如何在置換後重新排列字符串

public static void main(String[] args) { 

int[] IP = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 
      62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 
      57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 
      61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; 


String text = "00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111".replace(" ", ""); 

text = Permute(text,IP); 
System.out.println(text); 
} 

public static String Permute(String text,int [] table) 
{ 
    String keys = "0" + text; 

    StringBuilder sb = new StringBuilder(); 

    for (int i = 1 ; i <= table.length; i++) 
    { 
     sb.append(keys.charAt(table[i-1])); 
    } 

    return sb.toString(); 
} 

修訂:任何想法創造另一種方法來獲得原始字符串回來?喜歡的東西:

public static String GetoriginalText(String TextafterPermutation,int [] table) 
+4

保存其獨立的變量引用? ;-) – Andremoniy

+0

假設我丟失了原文。如何恢復? – FSm

+0

排列表可以包含兩倍相同的索引嗎? – Sebas

回答

1

您可以使用您根據表映射

public static String RePermute(final String text, final int[] table) { 
    final String keys = text; 

    char[] chararray = new char[table.length]; 

    for (int i = 0; i < keys.length() && i < table.length; i++) { 
     chararray[table[i]-1] = keys.charAt(i); 
    } 

    return new String(chararray); 
} 

此代碼是通過你的文字迭代填充chararray,並在你的表中指定的chararray地方將當前迭代的字符。

注意:您的代碼中存在一些問題,我保持原狀。 例如,我真的不明白爲什麼你總是把「0」放在你的文本前面。 你也可能想要處理傳遞的文本和表格長度不同的情況。

編輯:我刪除了在傳遞的文本前添加「0」的部分,而是將循環改爲從i = 0開始,而不是i = 1。

1

text = Permute(text,IP);要分配String對象從Permute函數返回的引用的聲明,所以其text變量拿着原來的基準是由Permute函數返回新參考取代。

如果你想保留text所持有的原始參考,只需使用其他變量,如String output = Permute(text,IP);

你可以使用一個簡單的程序如下所述:

public static String dePermute(String text, int[] table){ 

    String keys = text; 

    System.out.println(table.length); 
    String[] str = new String[table.length]; 

    for (int i = 1; i <= table.length; i++) { 
     System.out.println("Coming "+table[i - 1]); 
     str[table[i - 1]-1] = ""+keys.charAt(i-1); 
     //sb.append(keys.charAt(table[i - 1])); 
    } 


    return Arrays.toString(str).replace("[", "").replace("]", "").replace(",", "").replace(" ", ""); 
} 

您可以使用這一概念在這個節目解釋。我剛剛逆轉了Permute函數中您正在執行的過程。

+0

請閱讀我的更新請 – FSm

+0

非常感謝。無法接受更多的答案傷心 – FSm

1

您可以簡化算法使用字符數組,而不是StringBuilder

public static String permute(String text,int [] table) 
{ 
    char[] chars = new char[table.length]; 
    for (int i = 0 ; i < table.length; i++) { 
     chars[i] = text.charAt(table[i]-1); 
    } 

    return new String(chars); 
} 

之後逆向算法更爲明顯。你只需要進行反向分配:

public static String undo(String text,int [] table) { 
    char[] chars = new char[table.length]; 
    for (int i = 0; i < table.length; i++) 
    { 
     chars[table[i]-1] = text.charAt(i); 
    } 

    return new String(chars); 
} 
+0

非常感謝。無法接受更多的答案傷心 – FSm

1

假設你的置換表不改變,你可以這樣做:

public static String GetoriginalText(String TextafterPermutation,int [] table){ 

    char[] chars=new char[table.length]; 

    for(int i=0;i<table.length;i++){ 
    chars[table[i] - 1] = TextafterPermutation.charAt(i); 
    } 

    return new String(chars); 

} 
+0

非常感謝。很難接受更多的答案 – FSm

相關問題