acm编程竞赛题目2(Run Length Encoding)
Run Length EncodingDescriptionYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.
Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the
sequence, it is escaped with a 1, thus two 1 characters are output.
Input
The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input.
Output
Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.
输入样例
AAAAAABCCCC
12344
输出样例
6A1B14C
11123124
Source
Ulm Local 2004
example1:
#include<stdio.h>
#include<string.h>
void main()
{ int i,j,k,n;
char a;
gets(a);
n=strlen(a);
for(i=0;i<n-1; )
if(a==a)
{ for(j=i+1;a==a;j++);
printf("%d%c",j-i+1,a);
i=j+1;
}
else
{ if(a==1)
{ printf("11");
i++;
}
else
{ for(j=i+1;a!=a;j++);
printf("1");
if(j==n+1)
j--;
for(k=i;k<j;k++)
printf("%c",a);
printf("1");
i=j;
}
}
if(n==1)
if(a=='1')
printf("11");
else
printf("1%c1",a);
printf("\n");
}
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
#include<string.h>
void main()
{ int i,j,k,n;
char a;
gets(a);
n=strlen(a);
for(i=0;i<n-1; )
if(a==a)
{ for(j=i+1;a==a;j++);
printf("%d%c",j-i+1,a);
i=j+1;
}
else
{ if(a==1)
{ printf("11");
i++;
}
else
{ for(j=i+1;a!=a;j++);
printf("1");
if(j==n+1)
j--;
for(k=i;k<j;k++)
printf("%c",a);
printf("1");
i=j;
}
}
if(n==1)
if(a=='1')
printf("11");
else
printf("1%c1",a);
printf("\n");
} example2:#include<stdio.h>
#include<string.h>
void main()
{ int i,j,k,n;
char a;
gets(a);
n=strlen(a);
for(i=0;i<n-1; )
if(a==a)
{ for(j=i+1;a==a;j++);
printf("%d%c",j-i+1,a);
i=j+1;
}
else
{ if(a==1)
{ printf("11");
i++;
}
else
{ for(j=i+1;a!=a;j++);
printf("1");
if(j==n+1)
j--;
for(k=i;k<j;k++)
printf("%c",a);
printf("1");
i=j;
}
}
if(n==1)
if(a=='1')
printf("11");
else
printf("1%c1",a);
printf("\n");
}
example3:#include<stdio.h>
#include<string.h>
void main()
{ int i,j,k,n;
char a;
gets(a);
n=strlen(a);
for(i=0;i<n-1; )
if(a==a)
{ for(j=i+1;a==a;j++);
printf("%d%c",j-i+1,a);
i=j+1;
}
else
{ if(a==1)
{ printf("11");
i++;
}
else
{ for(j=i+1;a!=a;j++);
printf("1");
if(j==n+1)
j--;
for(k=i;k<j;k++)
printf("%c",a);
printf("1");
i=j;
}
}
if(n==1)
if(a=='1')
printf("11");
else
printf("1%c1",a);
printf("\n");
}
来源:编程爱好者acm题库 最好附上中文翻译嘛....很不起 难看 顶楼上…… 虽然没看懂,,,, 关注中!感兴趣的朋友都来说说
页:
[1]