HDU ACM 1020 Encoding

Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17349    Accepted Submission(s): 7456

Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:

1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

2. If the length of the sub-string is 1, '1' should be ignored.
 
Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
 
Output
For each test case, output the encoded string in a line.
 
Sample Input
2
ABC
ABBCCC
 
Sample Output
ABC
A2B3C
 
Author
ZHANG Zheng
 
Recommend
JGShining
 
#include<stdio.h>
#include<string.h>
char ch[10001];
int main()
{
    int t, i, j, k, len, count, n;
    char temp;
    scanf("%d", &n);
    for(t=1; t<=n; ++t)
    {
        getchar();
        memset(ch, 0, sizeof(ch));
        scanf("%s", ch);
        len = strlen(ch);    
          for(i=0,temp = ch[0],count=1; i<len-1; ++i )
          {
              
              if(temp != ch[i+1])
              {
                  if(count == 1)printf("%c",temp);
                  else printf("%d%c", count, temp);
                  temp = ch[i+1];
                  count = 1;
              }
              else ++count;

         }
         if(count == 1)printf("%c",temp);
                  else printf("%d%c", count, temp);
                  temp = ch[i+1];
                  count = 1;
         printf("\n");
        
    }
    return 0;
}

解题报告:

WA+1: 将字母好好地按字典排序并计算所有相同字幕的个数并输出;

WA+1: 提交错误后想到题目并无要求先要进行排序,修改代码后提交;

两次WA过后:一直理解不了这个单词的意思:sub-string. bing没有解决我的问题,无奈之下找AC代码,发现尽管是相同的字母,只要没有相连就要忽略,我已在怀疑AC代码的

正确性,提交后果断AC,我折服了! 急忙之下我修改自己的代码,两分钟左右吧,提交AC了。

原文地址:https://www.cnblogs.com/liaoguifa/p/2746613.html