Fork me on GitHub

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了。

posted @   Gifur  阅读(307)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示