[技术| 编程·课件·Linux] ACM趣题天天练 6 Encoding

simon3322 · 发布于 2012-09-04 09:42 · 1150 次阅读
997
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 100.


Output

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


Sample Input

2
ABC
ABBCCC


Sample Output

ABC
A2B3C

来自:http://acm.zju.edu.cn/  2478

评分

参与人数 1学分 +5 收起 理由
service + 5 原创帖子,对同学们很有帮助!

查看全部评分

共收到 3 条回复
yel_hb · #2 · 2012-9-4 13:41:03  回复 支持 反对
[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int n,i,len,k;
	string str;
	char tmp;

	while(cin >> n)
	{
		while(n--)
		{
			cin >> str;
			len = str.length();
			for(i = 0; i < len; ++i)
			{
				tmp = str[i];
				k = 1;
				while(str[++i] == tmp)
				{
					k++;
				}
				if(k != 1)
				{
					cout << k;
				}
				cout << tmp;
				i--;
			}
			cout << endl;
		}
	}

	return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 1学分 +12 收起 理由
simon3322 + 12 简单有效~!

查看全部评分

fghhslk · #3 · 2012-9-4 15:02:05  回复 支持 反对
[Java] 纯文本查看 复制代码
import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = Integer.parseInt(sc.nextLine());
                while (n-- > 0) {
                        int count;
                        char temp;
                        String str = sc.nextLine();
                        for (int i = 0; i < str.length(); i++) {
                                count = 0;
                                temp = str.charAt(i);
                                while (i < str.length() && str.charAt(i) == temp) {
                                        count++;
                                        i++;
                                }
                                if (count > 1) {
                                        System.out.print(count);
                                }
                                System.out.print(temp);
                                i--;
                        }
                        System.out.println();
                }
                sc.close();
        }
}

评分

参与人数 1学分 +9 收起 理由
simon3322 + 9

查看全部评分

poiu9080 · #4 · 2012-9-5 20:27:18  回复 支持 反对

  1. n = input()
  2. for a in range(n):
  3.     s = raw_input() + '\0'
  4.     res = ""
  5.     ch = 0
  6.     n = 0
  7.     for c in s:
  8.         if c == ch:
  9.             n += 1
  10.         elif c != ch:
  11.             if n == 1:
  12.                 res += ch
  13.             elif n > 1:
  14.                 res += str(n) + ch
  15.             ch = c
  16.             n = 1
  17.     print res
复制代码

评分

参与人数 1学分 +9 收起 理由
simon3322 + 9

查看全部评分

回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表