首页 > 其他 > 详细

杭电1020

时间:2016-01-12 22:49:50      阅读:149      评论:0      收藏:0      [点我收藏+]
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

 

 
水题,一开始误解了意思,以为是求一串字符串中相同字母的个数并输出,其实是求连续字母的个数并输出。
代码如下:
技术分享
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 10001
 4 int main(){
 5     int n=0;
 6     scanf("%d",&n);
 7     while(n--){
 8         char s[max];
 9         scanf("%s",s);
10         int count=1,
11             i;
12         for(i=0;i<strlen(s);i++){
13             if(s[i]==s[i+1])count++;
14             else{
15                 if(count==1)printf("%c",s[i]);
16                     else printf("%d%c",count,s[i]);
17                     count=1;
18             }
19         }
20         printf("\n");
21     }
22     return 0;
23 
24 }
View Code

 

杭电1020

原文:http://www.cnblogs.com/hongrunhui/p/5125740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!