描述
Bill is trying to compactly represent sequences of capital alphabetic characters from 'A' to 'Z' by folding repeating subsequences inside them. For example, one way to represent a sequence AAAAAAAAAABABABCCD is 10(A)2(BA)B2(C)D. He formally defines folded sequences of characters along with the unfolding transformation for them in the following way:
  • A sequence that contains a single character from 'A' to 'Z' is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
  • If S and Q are folded sequences, then SQ is also a folded sequence. If S unfolds to S' and Q unfolds to Q', then SQ unfolds to S'Q'.
  • If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. If S unfolds to S', then X(S) unfolds to S' repeated X times.

According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.
 
输入
The input contains a single line of characters from 'A' to 'Z' with at least 1 and at most 100 characters.
输出
Write to the output a single line that contains the shortest possible folded sequence that unfolds to the sequence that is given in the input file. If there are many such sequences then write any one of them.
样例输入
AAAAAAAAAABABABCCD
样例输出
9(A)3(AB)CCD
来源
Northeastern Europe 2002
本题的自定义校验器如下
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
//一些定义
const int ACCEPT = 0;
const int WRONG_ANSWER = 1;
//fstd 标准输出 fout 选手输出 fin 标准输入
FILE *fstd, *fout, *fin;
char a[205], b[205], c[205];
string d;

string unfold(int l, int r) {
	if (l > r) return "";
	string res = "";
	if (b[l] >= 'A' && b[l] <= 'Z') {
		res.push_back(b[l]);
		res.append(unfold(l + 1, r));
		return res;
	}
	if (b[l] >= '0' && b[l] <= '9') {
		int cnt = 0;
		for (; l <= r && b[l] >= '0' && b[l] <= '9'; l++) cnt = cnt * 10 + b[l] - '0';
		if (r - l < 2) exit(WRONG_ANSWER);
		if (cnt < 1) exit(WRONG_ANSWER);
		if (b[l] != '(') exit(WRONG_ANSWER);
		int mid = l + 1, k = 1;
		for (; mid <= r && k; mid++) {
			if (b[mid] == '(') k++;
			if (b[mid] == ')') k--;
		}
		if (mid - l < 3 || k) exit(WRONG_ANSWER);
		string sub = unfold(l + 1, mid - 2);
		while (cnt--) res.append(sub);
		res.append(unfold(mid, r));
		return res;
	}
	exit(WRONG_ANSWER);
}

int main(int argc, char* argv[]) {
	if (argc != 4) {
		printf("参数不足 %d", argc);
		return -1;
	}
	if (NULL == (fstd = fopen(argv[1], "r"))) {
		return -1;
	}
	if (NULL == (fout = fopen(argv[2], "r"))) {
		return -1;
	}
	if (NULL == (fin = fopen(argv[3], "r"))) {
		return -1;
	}
	fscanf(fin, "%s", a + 1);
	fscanf(fout, "%s", b + 1);
	fscanf(fstd, "%s", c + 1);
	if (strlen(b + 1) != strlen(c + 1)) return WRONG_ANSWER;
	d = unfold(1, strlen(b + 1));
	if (string(a + 1) != d) return WRONG_ANSWER;
	return ACCEPT;
}