Encoding
HDU - 1020
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.
InputThe 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.
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.
OutputFor each test case, output the encoded string in a line.
Sample Input
2 ABC ABBCCCSample Output
ABC A2B3C
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <algorithm> using namespace std; int main() { int t; char s[10005]; scanf("%d",&t); while(t--) { scanf("%s",s); int len=1; for(int i=0;i<strlen(s);i++) { if((s[i]!=s[i+1] && i<strlen(s)-1) || i==strlen(s)-1) { if(len!=1) printf("%d",len); printf("%c",s[i]); len=1; } else { while(s[i]==s[i+1] && i<strlen(s)-1) { len++; i++; } i--; } } printf("\n"); } return 0; }