HDU1020——Encoding

Encoding

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


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
 

Author
ZHANG Zheng
 
题意:
给定一个字符串,统计不同字符出现的次数,如果该字符出现次数大于1,就将后面的所有该字符换成该字符的数量。

解:
简单模拟一下就好。

#include <stdio.h>
#include <string.h>

char a[10005];

int main()
{
    memset(a,0,sizeof(a));

    int n;
    while(~scanf("%d",&n))
    {
        getchar();
        while(n--)
        {
            gets(a);
            int len=strlen(a);
            int count=0;
            for(int i=0;i<len;i++)
            {
                if(a[i]!=a[i+1])
                {
                    if(count>0)
                    {
                        printf("%d",count+1);
                    }
                    printf("%c",a[i]);
                    count=0;
                }
                else{
                    count++;
                }
            }
            printf("\n");
        }
    }
    return 0;
} 


网友netstarry写的一个处理字符编码的类,很好的解决了php中字符转换的问题 前一阵子见到了qiushuiwuhen君的关于gbk,unicode,big5的转换的文章 但是多少有一些不太大的问题 于是我设计了一个负责字符转换的类,修正了其中的一些不足,增加了部分功能,以后我会不断扩充该类,来支持更多的字符集 增加了如下几点: unicode->gbk 符号部分的转换 欧元符(?的识别 big5,Unicode,GBK之间的相互转换,前提是只转换共同的字符集部分, 使用说明: 暂时程序支持以下字符编码方式: GBK,BIG5,UTF-16BE(Unicode big-endian字节顺序),UTF-16LE(Unicode little-endian字节顺序),UTF-8 默认输入编码方式为GBK,默认输出编码方式为UTF-16BE; 该类别提供两个函数用来修改输入和输出编码方式: 修改输入编码方式 boolean SetGetEncoding(string $GetEncoding) 修改输出编码方式 boolean SetToEncoding(string $ToEncoding) 函数参数只能使用上述5种编码方式,区分大小写,如GBK不能写成gbk 如果设置成功,返回true,如果使用了错误的编码名,返回false,并现实错误信息 函数 string EncodeString(string $String) 负责进行字符编码转换,返回转换后的字符串 使用前,请将var $FilePath=\"\"变量该为该程序文件的绝对路径,否则将会找不到数据文件 例子: 将gbk编码的字符串转化为UTF-8编码: $s=\"GBK编码\"; $CharEncoding=new Encoding(); $CharEncoding->SetGetEncoding(\"GBK\")||die(\"编码名错误\"); $CharEncoding->SetToEncoding(\"UTF-8\")||die(\"编码名错误\"); echo $CharEncoding->EncodeString($s); 在浏览器中使用UTF-8编码察看,将会看到正确的字符.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值