zoukankan      html  css  js  c++  java
  • SDNU 1123.Encoding

    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
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <map>
    using namespace std;
    #define ll long long
    
    char c[10000+8];
    int n;
    
    int main()
    {
        scanf("%d", &n);
        for(int i = 0; i<n; i++)
        {
            scanf("%s",c);
            int ans = 0;
            int len = strlen(c);
            for(int j = 0; j<len-1; j++)
            {
                if(c[j] != c[j+1])
                {
                    if(ans)printf("%d", ans+1);
                    ans = 0;
                    printf("%c", c[j]);
                }
                else ans++;
            }
            if(ans)printf("%d%c
    ", ans+1, c[len-1]);
            else printf("%c
    ", c[len-1]);
        }
        return 0;
    }
  • 相关阅读:
    服务器切换
    闭包函数
    函数对象+嵌套
    lvs讲解
    了解python
    rang enumerate
    set-集合功能介绍
    元组-tuple功能介绍
    dict-字典功能介绍
    list-列表功能介绍
  • 原文地址:https://www.cnblogs.com/RootVount/p/10981691.html
Copyright © 2011-2022 走看看