zoukankan      html  css  js  c++  java
  • hdu 1020 Encoding

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

     

    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
     
     
    AC Code --Java
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		while (n-- != 0) {
    			String a = sc.next();
    			char[] arr = a.toCharArray();
    
    			char[] zc = new char[10000];
    			int[] zs = new int[5000];
    			int j = 0;
    			zc[0] = arr[0];
    			for (int i = 1; i < arr.length; i++) {
    				if (arr[i] == arr[i - 1]) {
    					zs[j]++;
    				} else {
    					zc[++j] = arr[i];
    				}
    			}
    
    			for (int i = 0; i < j + 1; i++) {
    				if (zs[i] == 0)
    					System.out.print(zc[i]);
    				else
    					System.out.print(zs[i] + 1 + "" + zc[i]);
    			}
    			System.out.println();
    		}
    	}
    }
    

     C 代码版

    #include <stdio.h>
    
    void out(char arr[100]);
    int main(void)
    {
    
        int n=0,i=0;
        char arr1[100][100];
        while(scanf("%d",&n)!=EOF)
        {
            getchar();
            for(i=0;i<n;i++)
            {
                gets(arr1[i]);
            }
    
            for(i=0;i<n;i++)
            {
                out(arr1[i]);
            }
        }
    
        return 0;
    }
    void out(char arr[100])
    {
        char s[100];
        int sum[100];
        int i=0,j=0;
        s[0]=arr[0];
        sum[0]=1;
        for(i=1;i<strlen(arr)+1;i++)
        {
            if(arr[i]==arr[i-1])
                sum[j]++;
            else
            {
                s[++j]=arr[i];
                sum[j]=1;
            }
        }
        s[j]='';
        for(i=0;i<strlen(s);i++)
            if(sum[i]!=1)
                printf("%d%c",sum[i],s[i]);
            else
                printf("%c",s[i]);
        printf("
    ");
    }
    

      

     
  • 相关阅读:
    c#读取excel导入到数据库中
    查找同一个表中某个字段中有相同的数据
    Url传值编码UrlEncode和解码UrlDecode
    FOR ALL ENTRIES IN
    设置自动增长列从哪里开始增长
    More than 100 ABAP Interview Faq's(1)
    使用__FILE__和__LINE__
    Windows API封装:LoadLibrary/FreeLibrary
    Singleton模式笔记
    C++接口着色技术
  • 原文地址:https://www.cnblogs.com/A--Q/p/5588838.html
Copyright © 2011-2022 走看看