uva 709 Formatting Text

import java.io.*;
import java.math.BigInteger;
import java.util.*;

class pair {

    int left, words, limit;

    public pair(int a, int b, int c) {
        left = a;
        words = b;
        limit = c;
    }

    public int get_cost() {
        int t1 = words;
        int t2 = left;
        int result = 0;
        if (left > 0 && words == 1) {
            return 500;
        }
        while (t2 > 0) {
            int t3 = t2 / (t1 - 1);
            result += t3 * t3;
            t2 -= t3;
            t1--;
        }
        return result;
    }
}

class problem {

    int n;

    void solver() throws IOException {
        Scanner scan = new Scanner(System.in);
        while (true) {
            n = Integer.parseInt(scan.nextLine());
            if (n == 0) {
                return;
            }
            ArrayList arr = new ArrayList();
            String line = null;
            while (!(line = scan.nextLine()).isEmpty()) {
                String tmp[] = line.split("\\s+");
                arr.addAll(Arrays.asList(tmp));
            }
            int s = arr.size();
            int dp[] = new int[s + 1];
            pair count[] = new pair[s + 1];
            for (int i = 0; i < s + 1; i++) {
                count[i] = new pair(n, 0, n);
            }
            Arrays.fill(dp, Integer.MAX_VALUE);
            dp[0] = 0;
            for (int i = 1; i <= arr.size(); i++) {
                for (int j = 1; j <= i; j++) {
                    int len = arr.get(i - 1).length();
                    for (int k = j; k  n) {
                        continue;
                    }
                    pair tmp = new pair(n, 0, n);
                    tmp.left = n-len;
                    tmp.words = i-j+1;

                    if (dp[j-1]+tmp.get_cost()0){
                            dp[i] = dp[j-1] + tmp.get_cost();
                            count[i] = tmp;                            
                        }
                    }
                }
            }
            backtrack(count,dp,s,arr);
            System.out.println();
        }
    }
    int back_judge(pair[] count, int[] dp, int p1, int p2){
        ArrayList a1 = new ArrayList();
        ArrayList a2 = new ArrayList();
        while(p1>=1){
            a1.add(count[p1].words);
            p1-=count[p1].words;
        }
        while(p2>=1){
            a2.add(count[p2].words);
            p2-=count[p2].words;
        }
        for(int i=Math.min(a1.size(), a2.size())-1;i>=0;i--){
            if(a1.get(i)!=a2.get(i)) return a1.get(i)-a2.get(i);
        }
        return 0;
    }
    void backtrack(pair[] count, int[] dp, int p, ArrayList arr){
        if(p>=1) backtrack(count,dp,p-count[p].words,arr);
        else return;
        pair tmp = count[p];
        StringBuilder sb = new StringBuilder();
        int t1=tmp.words-1;
        int t2=tmp.left;
        for(int i=p-tmp.words+1;i


这题真是,我都不好意思贴代码上来,写的太垃圾了

其实题目很简单的,如果只是求最小badness

解法说对于第i个字符串,看前面有K个能被拿下来和它放在一行,0<=k<=i-1

K=0时意味着第I个字符串单独一行,K=i-1就是说全部串放一行.

枚举K,那么可以求到最小的dp[k-1]+cost(k, i), 就是dp[i]的解

 

如果只是回溯一个最优解也好做,回溯一个特定(第一个gap最小的)的最优解就有点恶心了,

被逼无奈加了一个judge函数,把前面所有行的词数放入数组,比较第一个不同的,不同的那行越大越好。。。

 

google了一下一些别的解法,网上大多是用二维数组在搞,我只开了个一维数组,结果导致打印解很麻烦,

速度也不快. 不想尝试别的解法了,这题写得有点恶心。

 

你可能感兴趣的:(UVA,POJ)