Question: uva709-formatting text (recursive)
I will give you an article with many words. You are required to typeset and follow the length given by the question. Each line must start with a word and end with a word. If a row contains only one word, it is placed at the beginning. And the layout is based on the minimum badness.
Solution: it is difficult to think about the status of DP, but many details need to be considered. For example, if a row contains only one word, press enter directly and no space can be output, this PE is used. In addition, there is a separate line of a word. If the length of the word is smaller than the required length, badness = 500. What is required here is that the gap between the previous words is relatively small, then we need to go back from the back to the front. We also need to consider whether the word is placed in an optimal row separately, or whether the next word is better. In short, it is troublesome. If you are not careful, you will get wrong.
Status transition: DP [I] [J] (place the word I in the J position of the row where it is located (start position )) DP [I] [J-L [I] + k] = DP [I + 1] [J] + (k-1) * (k-1 ); K is the number of spaces. Note that J-L [I] + K must be between 0 -- l-l [I, in addition, K can be equal to 0 when J = 0, which is equivalent to the two words in two rows. In other cases, the value must be equal to at least 1. Then, you must consider your own rows separately.
Code:
# Include <cstdio> # include <cstring> const int n = 10005; const int M = 85; const int INF = 0x3f3f3f; int L, N; int DP [N] [m]; int P [N] [m]; // path char STR [N]; char word [N] [m]; int L [N]; // word length int min (const int A, const int B) {return a <B? A: B;} void handle () {process input Int J = 0; bool flag = 0; For (INT I = 0; I <= strlen (STR ); I ++) {If (STR [I]! = ''& STR [I]! = '\ 0') {word [N] [J ++] = STR [I]; flag = 1;} else {If (FLAG) {word [N] [J] = '\ 0'; L [n ++] = J; j = 0; flag = 0 ;}}}} void printf_ans (int x, int y) {// output path if (x = n + 1) return; If (! P [x] [Y] &! Y) {// enter printf ("% s", word [X-1]) without outputting spaces for a single line;} else {printf ("% s ", word [X-1]); If (X! = N) {for (INT I = Y + L [X-1]; I <p [x] [Y]; I ++) printf ("");}} if (! P [x] [Y] | x = N) printf ("\ n"); printf_ans (x + 1, P [x] [Y]);} int main () {int TMP; while (scanf ("% d % * C", & L), L) {n = 0; while (gets (STR) & STR [0]! = '\ 0') {handle () ;}// initfor (INT I = 0; I <= N; I ++) for (Int J = 0; j <= L; j ++) {DP [I] [J] = inf; P [I] [J] = L + 1 ;} DP [N] [0] = 500; P [N] [0] = 0; DP [N] [L-L [n-1] = 0; for (INT I = n-1; I> = 1; I --) {for (Int J = 0; j <= L-L [I]; j ++) {If (DP [I + 1] [J] = inf) continue; If (! J) {If (DP [I + 1] [J] <= DP [I] [L-L [I-1]) {// when two words are in two rows, DP [I] [L-L [I-1] = DP [I + 1] [J]; P [I] [L-L [I-1] = J;} TMP = (L [I-1] = L )? 0: 500; If (DP [I + 1] [J] + TMP <= DP [I] [0]) {// separate DP [I] [0] = DP [I + 1] [J] + TMP; P [I] [0] = J ;}} else {for (int K = 0; k <j-L [I-1]; k ++) {// TMP = J-L [I-1]-k-1; if (DP [I + 1] [J] + TMP * TMP <DP [I] [k]) {DP [I] [k] = DP [I + 1] [J] + TMP * TMP; P [I] [k] = J ;} else if (DP [I + 1] [J] + TMP * TMP = DP [I] [k]) {If (P [I] [k]> K) // if a word can be followed, do not separate a line of P [I] [k] = min (P [I] [K], J ); // Minimize the number of spaces in front of else P [I] [k] = J ;}}} printf_ans (1, 0); printf ("\ n ");} return 0 ;}
Uva709-formatting text (recursive)