Folding
Description
Bill is trying to compactly represent sequences of capital alphabetic characters from 'A' to 'Z' by fold ing repeating subsequences inside them. For example, one-to-represent a sequence 'aaaaaaaaaabababccd' is '(a) 2 (BA) B2 (C) D'. He formally defines folded sequences of characters along with the unfolding transformation for them in the following-in-the-loop:
- A sequence that contains a single character from 'a' to 'Z' are 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 and then X(S) are 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's 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-on-the-resulting folded sequence contains the least possible number of Characters.
Input
Input file contains several test cases, one per line. Each of them contains a single line of characters from 'a' to 'Z' with at least 1 and at the most Charac Ters.
Output
For each input case, write a different output line. This must was a single line, contains the shortest possible folded sequence that unfolds to the sequence that's given In the input file. If There is many such sequences then write any one of the them.
Sample Intput
AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES
Sample output
9 (A) 3 (AB) CCD2 (NEERC3 (YES)) Test instructions
Give you a string of strings that let you simplify Exercises
Interval DP
There are a few points to be aware of, namely that simplifying the number and two () may be longer than the original substring.
#include <bits / stdc ++. h>
using namespace std;
const int INF = 0x3f3f3f3f;
string str;
int DP [110] [110];
string fold [110] [110];
int judge (int l, int r) {
for (int i = 1; i <= (r-l + 1) / 2; i ++)
{
if ((r-l + 1)% i) continue;
bool flag = true;
for (int j = l; j + i <= r; j ++)
{
if (str [j]! = str [j + i])
{
flag = false;
break;
}
}
if (flag) return i;
}
return false;
}
int fun (int l, int r) {
if (DP [l] [r]! =-1) return DP [l] [r];
if (l == r) {
DP [l] [r] = 1;
fold [l] [r] = str [l];
return 1;
}
int k;
int re = INF;
for (int i = l; i <r; i ++)
{
int tmp = fun (l, i) + fun (i + 1, r);
if (tmp <re) {k = i; re = tmp;}
}
fold [l] [r] = fold [l] [k] + fold [k + 1] [r];
int len = judge (l, r);
if (len) {
char t [10];
sprintf (t, "% d", (r-l + 1) / len); // For an integer over ten, convert it to string
string newstr = t + string ("(") + fold [l] [l + len-1] + string (")");
if (newstr.size () <re) {
re = newstr.size ();
fold [l] [r] = newstr;
}
}
DP [l] [r] = re;
return re;
}
int main () {
while (cin >> str) {
int R = str.size ()-1;
memset (DP, -1, sizeof (DP));
fun (0, R);
cout << fold [0] [R] << endl;
}
return 0;
}
UVA1630 Folding interval DP