题目描述
Given a string containing only "A"-"Z", we could encode it using the following method:
We use 3 characters to represent a sub-string if there are some consecutive repeating
characters in one line, which "0" is as the mark, and then the repeat number , and the character itself.
输入
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 80.
输出
For each test case, output the encoded string in a line.
样例输入
2
ABBC
BBCCC
样例输出
A02BC
02B03C
关键:
The first line contains an integer N (1≤N ≤100) which indicates the number of test cases.
The next N lines contain N strings.
#include <stdio.h> // 处理每一行数据 void solve(char* str) { char ch; int count; while(*str){ //每个字符至少存在一个的 count = 1; //遍历与当前第1个字符相同的字符并计数 for(ch=*str++;*str&&*str==ch;str++){ count++; } if(count==1){//--------仅一次,输出原字符 printf("%c",ch); }else{//---------------最多3个,不足用零补足 printf("%02d%c",count,ch); } } printf("\n"); } int main(void) { //1<=N<=100,字符串长度<80 char astr[100][80]; int N,i; scanf("%d\n",&N); //按照题目要求,先读入所有待处理的数据 //不能读入一个就处理一个 for(i=0;i<N;i++){ gets(astr[i]); } //一次性处理所有输入数据 for(i=0; i<N; i++){ solve(astr[i]); } return 0; }
女孩不哭 @ 2013-05-08 22:24:30 @ http://www.cnblogs.com/nbsofer