d****n 发帖数: 233 | 1 Implement a encoding system as following:
Abckkkkkkkkkkks55p=> Abc11xk55p.
Rules:
encoded them as: [n]x[c]
where n is the repetition count and c is the actual character,
X is the special character.
Decoder side:
Any time above pattern is detected, it will output n number of c.
If x is the last character, output x.
How do you handle x in the original input? |
f*********s 发帖数: 34 | |
w*****d 发帖数: 105 | 3 好像没说一定要压缩,那可以这样:
x -> 1xx
xx -> 2xx
...
这样只要中间出现xx,就一定是[n]xx形式。 |
c******w 发帖数: 1108 | 4
为什么不是 Abc11xk2x5p ??
【在 d****n 的大作中提到】 : Implement a encoding system as following: : Abckkkkkkkkkkks55p=> Abc11xk55p. : Rules: : encoded them as: [n]x[c] : where n is the repetition count and c is the actual character, : X is the special character. : Decoder side: : Any time above pattern is detected, it will output n number of c. : If x is the last character, output x. : How do you handle x in the original input?
|
j********v 发帖数: 16 | 5 是不是
Abckkkkkkkkkkks55p => Abc11xk55p
Abckkkkkkkkkkks55sp => Abc11xk2x5p
11个k后面的s是什么意思? |
d****n 发帖数: 233 | 6 我的理解是不超过3个连续就没必要编码压缩,除非是x而且x不是最后一个字符。原文
中11个k后面的s是typo。
【在 j********v 的大作中提到】 : 是不是 : Abckkkkkkkkkkks55p => Abc11xk55p : Abckkkkkkkkkkks55sp => Abc11xk2x5p : 11个k后面的s是什么意思?
|
w****e 发帖数: 3827 | 7 如果是压缩的话,压完了长度要比原长度小,所以出现一两次的就不数个数了。
抛个砖,求轻拍
public static String encode(String s) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < s.length()) {
int count = 1;
while (i < s.length() - 1 && s.charAt(i) == s.charAt(i + 1)) {
i++;
count++;
}
if (count == 2) {
sb.append(s.charAt(i));
sb.append(s.charAt(i));
i++;
} else if (count > 2) {
sb.append(count);
sb.append('x');
//sb.append(s.charAt(i));
} else {
sb.append(s.charAt(i));
i++;
}
}
return sb.toString();
} |
M**********7 发帖数: 378 | 8 这题想来想去想不到很满意的方法。 顶一下看看大牛们怎么说。
1. 这种题decoder的逻辑会固定不能变么?感觉好像不能这么限制,否则好像就考的太
tricky了。
2. 本题除了x的转义还有数字也会有问题。比如91555555,如果按照定义直接上则
916x5有歧义。
3. 感觉总会有一些串不能保证结果比源串不大,比如源串为5xx。不能保证必小好证明
。不能保证至少一样,一下子想不出来好的证法。
4. 这种encoding decoding的题考什么呢?观察力和细心?压缩理论?有没有一些初步
的通用方法?或者这些都是给看中某背景的candidate的? |
p****a 发帖数: 447 | 9 可能只压字母,不压数字
【在 M**********7 的大作中提到】 : 这题想来想去想不到很满意的方法。 顶一下看看大牛们怎么说。 : 1. 这种题decoder的逻辑会固定不能变么?感觉好像不能这么限制,否则好像就考的太 : tricky了。 : 2. 本题除了x的转义还有数字也会有问题。比如91555555,如果按照定义直接上则 : 916x5有歧义。 : 3. 感觉总会有一些串不能保证结果比源串不大,比如源串为5xx。不能保证必小好证明 : 。不能保证至少一样,一下子想不出来好的证法。 : 4. 这种encoding decoding的题考什么呢?观察力和细心?压缩理论?有没有一些初步 : 的通用方法?或者这些都是给看中某背景的candidate的?
|