zoukankan      html  css  js  c++  java
  • 字符串编码(Encoding) ACM

    题目描述
         Given a string containing only "A"-"Z", we could encode it using the following method:
    We use 3 characters to represent a sub-string if there are some consecutive repeating
    characters in one line, which "0" is as the mark, and then the repeat number , and the character itself.

    输入
    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 80.

    输出
    For each test case, output the encoded string in a line.

    样例输入
    2
    ABBC
    BBCCC

    样例输出
    A02BC
    02B03C


      关键:
        The first line contains an integer N (1≤N ≤100) which indicates the number of test cases.
      The next N lines contain N strings.

    #include <stdio.h>
    
    // 处理每一行数据
    void solve(char* str)
    {
        char ch;
        int count;
    
        while(*str){
            //每个字符至少存在一个的
            count = 1;
            //遍历与当前第1个字符相同的字符并计数
            for(ch=*str++;*str&&*str==ch;str++){
                count++;
            }
            if(count==1){//--------仅一次,输出原字符
                printf("%c",ch);
            }else{//---------------最多3个,不足用零补足
                printf("%02d%c",count,ch);
            }
        }
        printf("\n");
    }
    
    int main(void)
    {
        //1<=N<=100,字符串长度<80
        char astr[100][80];
        int N,i;
        scanf("%d\n",&N);
        //按照题目要求,先读入所有待处理的数据
        //不能读入一个就处理一个
        for(i=0;i<N;i++){
            gets(astr[i]);
        }
        //一次性处理所有输入数据
        for(i=0; i<N; i++){
            solve(astr[i]);
        }
        return 0;
    }

     女孩不哭 @ 2013-05-08 22:24:30 @ http://www.cnblogs.com/nbsofer

  • 相关阅读:
    Linux/UNIX编程:实现简单 tee 命令
    Java原子变量类需要注意的问题
    一种很有意思的数据结构:Bitmap
    Java实现简单井字棋
    分治算法学习
    使用栈实现表达式求值
    Web安全学习笔记——SQL注入
    【old】Python学习笔记
    函数1
    pycharm(Tip of Day)
  • 原文地址:https://www.cnblogs.com/memset/p/3067948.html
Copyright © 2011-2022 走看看