杭电1020Encoding

地址: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!='
')
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,a);
43             else
44                 printf("%c
",a);
45     }
46     return 0;
47 }
View Code

作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

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