Run Length Encoding

2305 ワード

コースコード(Run Length Encoding)は、制御論における二値画像の符号化に一般的に用いられる簡単な符号化方法である.ACMには、このコードについての問題があります.tzu 1149またはpoj 1782を参照してください.简単な问题ですが、私は工夫を凝らしてやっとできました.功力はまだ足りません.
プログラムコードは次のとおりです.
#include <stdio.h>

#define MAX 1024

int printStr(char *str, int len);
int main(void)
{
  char target[MAX];
  int diffCount,sameCount;
  char *pFirstChar, *pLastChar;
  while(1)
    {
      if(fgets(target,sizeof(target),stdin) == NULL)
	break;
      pFirstChar = pLastChar = target;
      while(*pLastChar != '
') { diffCount = 0; sameCount = 1; /*if(*(pLastChar+1) == '
') { printStr(pLastChar,1); pLastChar++; } else*/ if(*pLastChar == *(pLastChar + 1)) { while(*pLastChar == *(pLastChar + 1) &&sameCount <= 8) { sameCount++; pLastChar++; } printf("%d%c",sameCount,*pLastChar); pLastChar++; pFirstChar = pLastChar; } else { while(*pLastChar != '
' && *pLastChar != *(pLastChar + 1)) { diffCount++; pLastChar++; } printStr(pFirstChar,diffCount); /* printf("
");*/ pFirstChar = pLastChar; } } if(*pLastChar == '
') putchar('
'); } return 0; } int printStr(char *str, int len) { int i; putchar('1'); for(i = 0; i < len; i++) if(*(str+i) == '1') printf("11"); else putchar(*(str+i)); putchar('1'); return 0; }