HDU 1020 Encoding 字符串操作

来源:互联网 发布:蜂群算法matlab 编辑:程序博客网 时间:2024/04/28 17:55

Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43902    Accepted Submission(s): 19397


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
2ABCABBCCC
 

Sample Output
ABCA2B3C
 

Author
ZHANG Zheng
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1062 1003 1039 1073 1108 
 
给你一个字符串然后让你改变一下输出格式,切记不能排序啊,排序就是错的。
ac代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;bool cmp(char x,char y){return x < y ? true : false;}int main(){int t,i,j,n,num,cnt[10005];char a[10005],b[10005],tmp;int place;while(~scanf("%d",&t)){while(t--){scanf("%s",a);//printf("%s\n",a);n=strlen(a);//sort(a,a+n,cmp);memset(cnt,0,sizeof(cnt));tmp=a[0];num=1;place=0;for(i=1;i<n;i++){if(a[i]==tmp)num++;else{b[place]=tmp;cnt[place]=num;place++;num=1;tmp=a[i];}}b[place]=tmp;cnt[place]=num;place++;for(i=0;i<place;i++){if(cnt[i]>1)printf("%d",cnt[i]);printf("%c",b[i]);}printf("\n");}}return 0;}

万里长城永不倒,再来一题好不好
1 0
原创粉丝点击