2015-11-03 31 views
2

問題: 運行長度編碼(RLE)是一種簡單的「壓縮算法」(一種算法,它採用一個數據塊並減小其大小,產生一個包含相同信息的塊更小的空間)。它通過用代表整個序列的短「代幣」來代替相同數據項的重複序列。將RLE應用於字符串涉及在相同字符重複的字符串中查找序列。每個這樣的序列應該由「令牌」替換爲:編寫程序壓縮字符串

the number of characters in the sequence 
the repeating character 

如果一個字符不重複,它應該被單獨留下。

例如,請考慮以下字符串:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT 

應用RLE算法後,這個字符串轉換成:

q9w5e2rt5y4qw2Er3T 

這是我到目前爲止,我不知道知道如何計算角色的重複次數。有人可以幫助!!!!

public class Compress1 { 
    public static void main(String[] args){ 
     System.out.println("Enter a string"); 
     String input = IO.readString(); 
     char[] inputChar = input.toCharArray(); 
     for (int index = 0; index < inputChar.length; index++){ 
      char current = inputChar[index]; 

      if (current == (current + 1)){ 
       int count = 
      } 
     } 
    } 
} 

回答

1

可以嘗試這:

String str = "qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT"; 
char[] arr = str.toCharArray(); 
int count = 1; 
StringBuilder sb = new StringBuilder(); 
char prev = arr[0]; 

for (int i = 1; i < arr.length; i++) { 
    char curr = arr[i]; 
    prev = arr[i - 1]; 
    if (curr == prev) { 
     count++; 
    } else { 
     if (count < 2) { 
      sb.append(prev); 
     } else { 
      sb.append(count).append(prev); 
      count = 1; 
     } 
    } 
} 

if (count < 2) { 
    sb.append(prev); 
} else { 
    sb.append(count).append(prev); 
} 

System.out.println("Compressed : " + sb.toString()); 

輸出:Compressed : q9w5e2rt5y4qw2Er3T

0

這是您的代碼的工作版本。它沿着一個字符逐個輸入字符串。如果下一個字符是不同於前一個字符的,則它打印該字符的RLE版本(即,如果字符只出現一次,則爲字符,或者如果它出現多次,則爲字符後跟字符)。如果下一個字符是與前一個字符相同的相同的,那麼它增加一個計數器以跟蹤發生的次數。

如果仔細觀察,您會注意到我在輸入字符串的末尾附加了一個新行(\n)。這允許我的算法「知道」它已經到達輸入字符串的末尾。如果您打算爲RLE處理輸入多個字符串,它也會很好地打印換行符。

在某些時候,您可能想要提取出一個執行RLE處理的方法。現在,我已將代碼保留在main()方法中。

public class Compress1 { 
    public static void main(String[] args){ 
     System.out.println("Enter a string"); 
     String input = IO.readString(); 

     // check for null or empty input 
     if (input == null || input.length() == 0) { 
      System.out.println("null or empty string input"); 
      System.exit(0); 
     } 

     // handle single character input 
     if (input.length() == 1) { 
      System.out.println(String.valueOf(curr)); 
      System.exit(0); 
     } 

     // add a newline character to the end of the input string 
     // so the algorithm can detect a "change" at the end 
     input += "\n"; 

     char curr = input.charAt(0); 
     int count=1; 

     for (int i=1; i < input.length(); ++i) { 
      char next = input.charAt(i); 
      if (curr != next) { 
       if (count > 1) { 
        System.out.print(count + String.valueOf(curr)); 
       } 
       else { 
        System.out.print(curr); 
       } 
       count = 1; 
      } 
      else { 
       ++count; 
      } 

      curr = next; 
     } 
    } 
} 

下面是一個示例的輸入和輸出,我使用測試的IntelliJ:

輸入:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT 

輸出:

q9w5e2rt5y4qw2Er3T 
2

而不是調試代碼,這裏的一些工作代碼:

String input = "qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT"; 

String previous = input.substring(0, 1); 
int count = 1; 
for (String c : input.substring(1).split("")) { 
    if (previous.equals(c)) { 
     count++; 
    } else { 
     System.out.print((count == 1 ? "" : count) + previous); 
     previous = c; 
     count = 1; 
    } 
} 
System.out.println((count == 1 ? "" : count) + previous); 

輸出:

q9w5e2rt5y4qw2Er3T 

用自己比較它,並按照其邏輯找出你出錯的地方。

0
public static void main (String[] args) { 
    System.out.println("Enter a string"); 
    String input = IO.readString(); 
    final char[] charArray = input.toCharArray(); 
    final StringBuilder stringBuilder = new StringBuilder(); 
    int index = 1; 
    for (int current = 1, previous = 0; current <= charArray.length; current++, previous++) { 
     if (current == charArray.length || charArray[previous] != charArray[current]) { 
      if (index > 1) { 
       stringBuilder.append(index); 
      } 
      stringBuilder.append(charArray[previous]); 
      index = 1; 
     } else { 
      index++; 
     } 
    } 
    System.out.println(stringBuilder.toString()); 
}