列表

详情


NC51220. Folding

描述

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: 
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.

示例1

输入:

AAAAAAAAAABABABCCD

输出:

9(A)3(AB)CCD

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 6ms, 内存消耗: 1272K, 提交时间: 2020-08-04 15:49:30

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#include <bitset>

#define INF 0x3f3f3f3f
#define IMAX 2147483646;
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace::std;

const int N = 106;
struct F {
	char s[N];
	int len;
} f[N][N];
char s[N];

int main() {
	scanf("%s", s);
	int n = strlen(s);
	for (int i = 0; i < n; i++) {
		f[i][i].len = 1;
		f[i][i].s[0] = s[i];
	}
	for (int len = 2; len <= n; len++)
		for (int l = 0; l + len <= n; l++) {
			int r = l + len - 1;
			f[l][r].len = INF;
			for (int i = 1; i <= len >> 1; i++) {
				if (len % i) continue;
				int L = l, R = l + i;
				while (R <= r && s[L++] == s[R]) ++R;
				if (R > r) {
					sprintf(f[l][r].s, "%d", len / i);
					strcat(f[l][r].s, "(");
					strcat(f[l][r].s, f[l][l + i - 1].s);
					strcat(f[l][r].s, ")");
					f[l][r].len = strlen(f[l][r].s);
					break;
				}
			}
			for (int i = l; i < r; i++)
				if (f[l][r].len > f[l][i].len + f[i + 1][r].len) {
					f[l][r].len = f[l][i].len + f[i + 1][r].len;
					strcpy(f[l][r].s, f[l][i].s);
					strcat(f[l][r].s, f[i + 1][r].s);
				}
		}
	puts(f[0][n - 1].s);
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 11ms, 内存消耗: 1400K, 提交时间: 2020-05-04 22:16:25

//Author:XuHt
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 106, INF = 0x3f3f3f3f;
struct F {
	char s[N];
	int len;
} f[N][N];
char s[N];

int main() {
	scanf("%s", s);
	int n = strlen(s);
	for (int i = 0; i < n; i++) {
		f[i][i].len = 1;
		f[i][i].s[0] = s[i];
	}
	for (int len = 2; len <= n; len++)
		for (int l = 0; l + len <= n; l++) {
			int r = l + len - 1;
			f[l][r].len = INF;
			for (int i = 1; i <= len >> 1; i++) {
				if (len % i) continue;
				int L = l, R = l + i;
				while (R <= r && s[L++] == s[R]) ++R;
				if (R > r) {
					sprintf(f[l][r].s, "%d", len / i);
					strcat(f[l][r].s, "(");
					strcat(f[l][r].s, f[l][l+i-1].s);
					strcat(f[l][r].s, ")");
					f[l][r].len = strlen(f[l][r].s);
					break;
				}
			}
			for (int i = l; i < r; i++)
				if (f[l][r].len > f[l][i].len + f[i+1][r].len) {
					f[l][r].len = f[l][i].len + f[i+1][r].len;
					strcpy(f[l][r].s, f[l][i].s);
					strcat(f[l][r].s, f[i+1][r].s);
				}
		}
	puts(f[0][n-1].s);
	return 0;
}

上一题