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

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
作者:雪影蓝枫
本文版权归作者和博客园共有,欢迎转载,未经作者同意须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?