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
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

const double inf=1e20;
const int maxn=1e5+10;
const int mod=1e9+7;

char a[maxn];
char s[maxn];
int num[maxn];

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int len=strlen(a);
        int j=0;
        s[0]=a[0];
        num[0]=1;
        for(int i=1;i<len;i++){
            if(a[i]==s[j])num[j]++;
            else {
                s[++j]=a[i];
                num[j]=1;
            }
        }
        for(int i=0;i<=j;i++){
            if(num[i]!=1){
                printf("%d",num[i]);
            }
            printf("%c",s[i]);
        }printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wz-archer/p/12405531.html