码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1020 Encoding 字符统计

时间:2016-02-03 11:35:47      阅读:219      评论: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
 
水题之一,就是给你n串字符,按顺序输出字母连续出现的次数。
表示一开始将数字和字母位置弄反了,导致一直WA的我真是无药可救了。
代码如下:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 char ch[15000],before;
 7 int num,n;
 8 int main(){
 9     scanf("%d",&n);
10     while(n!=0)
11     {
12         n--;
13         scanf("%s",ch);
14         int len=strlen(ch);
15         before=ch[0];
16         num=1;
17         for (int i=1;i<len;++i)
18         {
19             if (ch[i]==before)
20             num++;
21             else
22             {
23                 if (num!=1)
24                 printf("%d%c",num,before);
25                 else
26                 printf("%c",before);
27                 num=1;
28                 before=ch[i];
29             }
30             
31         }if (num!=1)
32                 printf("%d%c\n",num,before);
33                 else
34                 printf("%c\n",before);
35         
36     }
37     return 0;
38 }

 

HDU 1020 Encoding 字符统计

标签:

原文地址:http://www.cnblogs.com/fakerv587/p/5179200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!