运行长度编码
给定一个输入字符串,编写一个函数,返回输入字符串。
例如,如果输入字符串是“wwwwaaadexxxxxx”,那么函数应该返回“w4a3d1e1x6”
a) 从源字符串中选择第一个字符。
b) 将选取的字符附加到目标字符串。
c) 计算所选字符的后续出现次数,并将计数附加到目标字符串。
d) 如果没有到达字符串的末尾,则选择下一个字符并重复步骤 b) c) 和 d)。
C++
// CPP program to implement run length encoding
#include
using namespace std;
void printRLE(string str)
{
int n = str.length();
for (int i = 0; i < n; i++) {
// Count occurrences of current character
int count = 1;
while (i < n - 1 && str[i] == str[i + 1]) {
count++;
i++;
}
// Print character and its count
cout << str[i] << count;
}
}
int main()
{
string str = "wwwwaaadexxxxxxywww";
printRLE(str);
return 0;
}
Python3
# Python3 program to implement
# run length encoding
def printRLE(st):
n = len(st)
i = 0
while i < n- 1:
# Count occurrences of
# current character
count = 1
while (i < n - 1 and
st[i] == st[i + 1]):
count += 1
i += 1
i += 1
# Print character and its count
print(st[i - 1] +
str(count),
end = "")
# Driver code
if __name__ == "__main__":
st = "wwwwaaadexxxxxxywww"
printRLE(st)
# This code is contributed by Chitranayal
C#
// C# program to implement run length encoding
using System;
class GFG
{
public class RunLength_Encoding
{
public static void printRLE(String str)
{
int n = str.Length;
for (int i = 0; i < n; i++)
{
// Count occurrences of current character
int count = 1;
while (i < n - 1 && str[i] == str[i + 1])
{
count++;
i++;
}
// Print character and its count
Console.Write(str[i]);
Console.Write(count);
}
}
public static void Main(String[] args)
{
String str = "wwwwaaadexxxxxxywww";
printRLE(str);
}
}
}
// This code is contributed by shivanisinghss2110
Javascript
C
#include
#include
#include
#define MAX_RLEN 50
/* Returns the Run Length Encoded string for the
source string src */
char* encode(char* src)
{
int rLen;
char count[MAX_RLEN];
int len = strlen(src);
/* If all characters in the source string are different,
then size of destination string would be twice of input string.
For example if the src is "abcd", then dest would be "a1b1c1d1"
For other inputs, size would be less than twice. */
char* dest = (char*)malloc(sizeof(char) * (len * 2 + 1));
int i, j = 0, k;
/* traverse the input string one by one */
for (i = 0; i < len; i++) {
/* Copy the first occurrence of the new character */
dest[j++] = src[i];
/* Count the number of occurrences of the new character */
rLen = 1;
while (i + 1 < len && src[i] == src[i + 1]) {
rLen++;
i++;
}
/* Store rLen in a character array count[] */
sprintf(count, "%d", rLen);
/* Copy the count[] to destination */
for (k = 0; *(count + k); k++, j++) {
dest[j] = count[k];
}
}
/*terminate the destination string */
dest[j] = '\0';
return dest;
}
/*driver program to test above function */
int main()
{
char str[] = "geeksforgeeks";
char* res = encode(str);
printf("%s", res);
getchar();
}
输出:
w4a3d1e1x6y1w3
C 实现
C
#include
#include
#include
#define MAX_RLEN 50
/* Returns the Run Length Encoded string for the
source string src */
char* encode(char* src)
{
int rLen;
char count[MAX_RLEN];
int len = strlen(src);
/* If all characters in the source string are different,
then size of destination string would be twice of input string.
For example if the src is "abcd", then dest would be "a1b1c1d1"
For other inputs, size would be less than twice. */
char* dest = (char*)malloc(sizeof(char) * (len * 2 + 1));
int i, j = 0, k;
/* traverse the input string one by one */
for (i = 0; i < len; i++) {
/* Copy the first occurrence of the new character */
dest[j++] = src[i];
/* Count the number of occurrences of the new character */
rLen = 1;
while (i + 1 < len && src[i] == src[i + 1]) {
rLen++;
i++;
}
/* Store rLen in a character array count[] */
sprintf(count, "%d", rLen);
/* Copy the count[] to destination */
for (k = 0; *(count + k); k++, j++) {
dest[j] = count[k];
}
}
/*terminate the destination string */
dest[j] = '\0';
return dest;
}
/*driver program to test above function */
int main()
{
char str[] = "geeksforgeeks";
char* res = encode(str);
printf("%s", res);
getchar();
}
输出:
g1e2k1s1f1o1r1g1e2k1s1