代码实现:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char ch, *str = (char*)malloc(sizeof(char) * 1100000);
int count = 0;
scanf("%c", &ch); getchar(); gets(str);
if (ch == 'C')
{
char *str1 = (char*)malloc(sizeof(char) * 1200000);
for (int i = 0; str[i] != 0; i++)
{
int cnt = 0, j;
if (str[i] == str[i + 1])
{
for (j = i; str[j] == str[i]; j++)
cnt++;
i = j - 1;
if (cnt < 10)
str1[count++] = cnt + '0', str1[count++] = str[i];
else
{
int number = 0, cnt1 = cnt;
do
{
number++;
cnt1 /= 10;
} while (cnt1 != 0);
while (number != 0)
{
int ten = 1;
for (int k = 0; k < number - 1; k++)
ten *= 10;
str1[count++] = (cnt / ten) % 10 + '0';
number--;
}
str1[count++] = str[i];
}
}
else str1[count++] = str[i];
}
str1[count] = 0;
puts(str1);
}
else
{
char *str1 = (char*)malloc(sizeof(char) * 200000);
for (int i = 0; str[i] != 0; i++)
if (str[i] >= '0'&&str[i] <= '9')
{
int cnt = 0, j;
for (j = i; str[j] >= '0'&&str[j] <= '9'; j++)
{
cnt += str[j] - '0';
if (str[j + 1] >= '0'&&str[j + 1] <= '9')
cnt *= 10;
}
i = j;
for (int k = 0; k < cnt; k++)
str1[count++] = str[i];
}
else
str1[count++] = str[i];
str1[count] = 0;
puts(str1);
}
return 0;
}