• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

HDU 1020 Encoding 字符统计

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 }

 

posted @ 2016-02-03 10:49  ♕Heart-broken  阅读(159)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3