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
#include <cstdio>
#include <iostream>
#include <cmath>
#include <
string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
char c[
10000+
8];
int n;
int main()
{
scanf("%d", &
n);
for(
int i =
0; i<n; i++
)
{
scanf("%s",c);
int ans =
0;
int len =
strlen(c);
for(
int j =
0; j<len-
1; j++
)
{
if(c[j] != c[j+
1])
{
if(ans)printf(
"%d", ans+
1);
ans =
0;
printf("%c", c[j]);
}
else ans++
;
}
if(ans)printf(
"%d%c\n", ans+
1, c[len-
1]);
else printf(
"%c\n", c[len-
1]);
}
return 0;
}
转载于:https://www.cnblogs.com/RootVount/p/10981691.html
相关资源:JAVA上百实例源码以及开源项目