2012-07-11 61 views
0

我不知道如何開始我的任務。java遊程編碼

我們必須做出遊程編碼程序,

例如,用戶輸入字符串:

aaaaPPPrrrrr

被替換

4a3P5r

燦有人幫我開始使用它?

+3

[你有什麼嘗試](http://www.whathaveyoutried.com/)?你不知道/理解什麼? – Pshemo 2012-07-11 22:03:18

+0

你能幫我編碼嗎?像循環一樣 – Shawn 2012-07-11 22:34:44

+0

忘掉Java。只需坐下來思考你將要使用的過程並用英語寫下來(或者你喜歡的任何口語)。使用幾個不同的(和不同的)例子「執行」那個序列,當你覺得自己沒有錯的時候,就轉化成Java。 – 2012-10-05 16:39:41

回答

4

希望這將讓你開始你的任務:

後面的遊程編碼的基本思想是,像AAAA相繼出現的令牌可以通過較短的形式4A(意爲「以下四個字符是一個取代'一個'」)。這種類型的編碼在計算機圖形學的早期階段被用來在存儲圖像時節省空間。當時,顯卡支持的色彩和圖像的少數普遍有同樣的顏色都在一排

你可以在它的細節在維基百科上

http://en.wikipedia.org/wiki/Run-length_encoding

讀了圖像的顯著部分)

爲了對字符串進行運行長度編碼,您可以遍歷輸入字符串中的字符。有一個計數器可以統計你連續看過同一個字符的次數。當你看到不同的字符時,輸出計數器的值,然後輸出你計算的字符。如果計數器的值爲1(表示只能看到一行中的某個字符),則跳過輸出計數器。

+0

一些東西是在先進的。我還沒有學到那麼多。 – Shawn 2012-07-11 22:34:17

+1

嘗試實現我在最後一段中描述的代碼。當你有最好的代碼時,可以把它作爲一個新問題發佈,並解釋你所困擾的問題。在你的新問題中引用這個問題。 – 2012-07-11 23:49:09

8
public static String encode(String source) { 
    StringBuffer dest = new StringBuffer(); 
    for (int i = 0; i < source.length(); i++) { 
     int runLength = 1; 
     while (i + 1 < source.length() 
       && source.charAt(i) == source.charAt(i + 1)) { 
      runLength++; 
      i++; 
     } 
     dest.append(runLength); 
     dest.append(source.charAt(i)); 
    } 
    return dest.toString(); 
} 

public static String decode(String source) { 
    StringBuffer dest = new StringBuffer(); 
    Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]"); 
    Matcher matcher = pattern.matcher(source); 
    while (matcher.find()) { 
     int number = Integer.parseInt(matcher.group()); 
     matcher.find(); 
     while (number-- != 0) { 
      dest.append(matcher.group()); 
     } 
    } 
    return dest.toString(); 
} 

public static void main(String[] args) { 
    String example = "aaaaabbbbbcccccr"; 
    System.out.println(encode(example)); 
    System.out.println(decode(encode(example))); 
} 
} 

這應該做你的工作:)享受

+1

您的解決方案看起來像它的工作,但似乎過於複雜(O(n^2)),而不是一個更簡單的迭代O(n)解決方案。 – Leonidas 2013-05-15 03:12:45

+1

只是一個猜測,但這顯然是一個家庭作業的問題,只是給codez教幾乎沒有任何教導:因此倒票。 – 2014-09-24 12:44:29

+2

這個答案是從這裏複製的: http://rosettacode.org/wiki/Run-length_encoding#Java – 2014-10-26 14:44:50

1
public String runLengthEncoding(String text) { 
    String encodedString = ""; 

    for (int i = 0, count = 1; i < text.length(); i++) { 
     if (i + 1 < text.length() && text.charAt(i) == text.charAt(i + 1)) 
      count++; 
     else { 
      encodedString = encodedString.concat(Integer.toString(count)) 
        .concat(Character.toString(text.charAt(i))); 
      count = 1; 
     } 
    } 
    return encodedString; 
} 

試試這個。

0
import java.util.Scanner; 
/** 
* @author jyotiv 
* 
*/ 
public class RunLengthEncoding { 
/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    System.out.println("Enter line to encode:"); 
    Scanner s=new Scanner(System.in); 
    String input=s.nextLine(); 
      int len = input.length(); 
      int i = 0; 
      int noOfOccurencesForEachChar = 0; 
      char storeChar = input.charAt(0); 

      String outputString = ""; 
      for(;i<len;i++) 
      { 
       if(i+1<len) 
       { 
        if(input.charAt(i) == input.charAt(i+1)) 
        { 
         noOfOccurencesForEachChar++; 
        } 
        else 
        { 
         outputString = outputString + 
    Integer.toHexString(noOfOccurencesForEachChar+1) + storeChar; 
         noOfOccurencesForEachChar = 0; 
         storeChar = input.charAt(i+1); 
        } 
       } 
       else 
       { 
        outputString = outputString + 
Integer.toHexString(noOfOccurencesForEachChar+1) + storeChar; 
       } 
      } 

      System.out.println("Encoded line is: " + outputString); 

     } 

} 

我試過這個。它肯定會起作用。

0

這可以輕鬆簡單地使用StringBuilder和一些輔助變量來完成,以跟蹤每個字母的數量。然後就像你一樣建造。

例如:

static String encode(String s) { 
    StringBuilder sb = new StringBuilder(); 
    char[] word = s.toCharArray(); 
    char current = word[0]; // We initialize to compare vs. first letter 

      // our helper variables 
    int index = 0; // tracks how far along we are 
    int count = 0; // how many of the same letter we've seen 

    for (char c : word) { 
     if (c == current) { 
      count++; 
      index++; 

      if (index == word.length) 
       sb.append(current + Integer.toString(count)); 
     } 

     else { 
      sb.append(current + Integer.toString(count)); 
      count = 1; 
      current = c; 
      index++; 
     } 
    } 
    return sb.toString(); 
} 

因爲這顯然是一個家庭作業,我挑戰你的學習方法,而不僅僅是簡單地使用答案解決你的功課。 StringBuilders對於構建事物非常有用,因此在很多情況下保持運行時O(n)。這裏使用一對幫助變量來跟蹤我們在迭代「索引」中的位置,另一個用於保持計數我們已經看到「計數」的特定字母的數量,因此我們保留了構建編碼字符串的所有必要信息,因爲我們走。