字符串 (扫一遍 + 计数)
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
小结:比赛时,题意理解错了,以为是扫完一遍后,再输出字母的个数,比如 BBAABB, 按我错误的code应该是输出 2A4B, 而实际应该是输出 2B2A2B。 赛后问了学长一下题意,10minAC了,郁闷。。。(写这个题是为提醒自己要认真读题,不能只看输入输出,就盲目敲)
#include<stdio.h> #include<string.h> int main() { int n, i, count=0; char a[10005]; scanf("%d", &n); while(n--) { scanf("%s", a); int len = strlen(a); for(i=0; i<len; i++) { if(a[i] == a[i+1]) count++; else { if(count == 0) printf("%c", a[i]); else printf("%d%c", count+1, a[i]); count = 0; } } printf("\n"); } return 0; }
每天训练发现我比别人做的好慢,但是理解的更深刻,如果一开始学一个新知识点就搜模板,那么这样的人是走不远的,毕业之后带走的只有思维,什么荣誉,奖杯都已经不重要了。