HDU1020 Encoding

http://acm.hdu.edu.cn/showproblem.php?pid=1020

题意 :给定一个字符串,只包含大写字母,如果字符串中又连续相同的字母,个数大于1的话,就输出该字母连续出现了几个,再加上这个字母,如果只出现一次的话就输出该字母即可。例如ABBCCC,A只出现了一次,就输出A,B出现了两次,就输出2B,C出现了三次,就输出3C。

思路 :本来我想用哈希来着,结果想想不对,不用那么麻烦,又改了,因为字符串不是很长,所以从头开始找就行。一定要是连续的,ABAG,输出ABAG。

//HDU1020
#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

char ch[11000] ;
int sh[11000] ;

int main()
{
    int n ;
    scanf("%d",&n) ;
    for(int i = 0 ; i < n ; i++)
    {
        scanf("%s",ch) ;
        int len = strlen(ch);
        for(int j = 0 ; j < len ; )
        {
            int cnt = 1 ;
            for(int k = j+1 ; k < len ; k++)
            {
                if(ch[j] == ch[k])
                cnt++ ;
                else
                break ;
            }
            if(cnt > 1)
            {printf("%d%c",cnt,ch[j]) ;
            j += cnt ;
            }
            else
            {printf("%c",ch[j]) ;j++ ;}
        }
        cout<<endl ;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3438652.html