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

杭电1020Encoding

时间:2016-04-04 14:36:12      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1020

题目:

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 <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <queue>
 7 #include <stack>
 8 #include <map>
 9 #include <vector>
10 
11 #define PI acos((double)-1)
12 #define E exp(double(1))
13 using namespace std;
14 
15 int main (void)
16 {
17     int t,n;
18     char a,b;
19     cin>>t;
20     scanf("%*c");
21     while(t--)
22     {
23         n=1;
24         scanf("%c",&a);
25         while(scanf("%c",&b)==1 && b!=\n)
26         {
27             if(a == b)
28             {
29                     n++;
30             }
31             else
32             {
33                 if(n>1)
34                     printf("%d%c",n,a);
35                 else
36                     printf("%c",a);
37                 n=1;
38                 a=b;
39             }
40         }
41         if(n>1)
42                 printf("%d%c\n",n,a);
43             else
44                 printf("%c\n",a);
45     }
46     return 0;
47 }
View Code

 

杭电1020Encoding

标签:

原文地址:http://www.cnblogs.com/weeping/p/5351848.html

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