您的位置:首页 > Web前端

uva 11552 - Fewest Flops(dp)

2012-12-12 13:59 459 查看
题目链接http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2547
Problem F

FEWEST FLOPS

A common way to uniquely encode a string is by replacing its consecutive repeating characters (or “chunks”) by the number of times the character occurs followed by the character itself. For example, the string
“aabbbaabaaaa” may be encoded as “2a3b2a1b4a”. (Note for this problem even a single character “b” is replaced by “1b”.)

Suppose we have a string S and a number
k such that k divides the length of S. Let S1 be the substring of
S from 1 to k, S2 be the substring of
S from k + 1 to 2k, and so on. We wish to rearrange the characters of each block
Si independently so that the concatenation of those permutations
S’ has as few chunks of the same character as possible. Output the fewest number of chunks.

For example, let S be “uuvuwwuv” and
k be 4. Then S1 is “uuvu” and has three chunks, but may be rearranged to “uuuv” which has two chunks. Similarly,
S2 may be rearranged to “vuww”. Then S’, or S1S2, is “uuuvvuww” which is 4 chunks, indeed the minimum number of chunks.

Program Input

The input begins with a line containing
t (1 ≤ t ≤ 100), the number of test cases. The following t
lines contain an integer k and a string S made of no more than 1000 lowercase English alphabet letters. It is guaranteed that
k will divide the length of S.

Program Output

For each test case, output a single line containing the minimum number of chunks after we rearrange
S as described above.

INPUT

2
5 helloworld
7 thefewestflops

OUTPUT
8
10


Calgary Collegiate Programming Contest 2008



在每个块的内部,显然是采用贪心的思想,将相同字母都放在一起

而最后将块合并时,起决定性的显然是每个块的首部和尾部分别是什么字母,于是每个块就有An2-n(除了n为1的情况,其他中首尾字母是不能相同的)种不同情况

这样按块划分成不同阶段,每个阶段只与前面一个阶段相关

因为不确定每个块中有多少种情况,所以用了vector

代码如下

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct Node{
    char s,e;
    int num;
};
vector <Node> vdp[1005];
set <char> s;
set<char>::iterator it;

int main(){
    int t,partlen;
    char text[1005];
    int kind[1005];//记录每个块中有多少种情况
    cin >> t;
    while(t--){
        for(int i = 0;i < 1005;i++)
            vdp[i].clear();
        s.clear();
        cin >> partlen >> text;
        int len = strlen(text);
        int partnum = len / partlen;
        //先预处理出每个块的不同元素的个数
        //并将各种组合情况放到vector中
        int p = 0,count = 0;
        while(p < len){
            s.insert(text[p]);
            if(p % partlen == partlen-1){
                int tsize = s.size();
                if(tsize == 1){
                    kind[count] = 1;
                    Node temp;
                    temp.s = *s.begin();
                    temp.e = temp.s;
                    temp.num = 1;
                    vdp[count].push_back(temp);
                }
                else{
                    //先将该集合中的元素放到tems数组中
                    char tems[1005];
                    int ti = 0;
                    for(it = s.begin();it != s.end();it++){
                        tems[ti++] = *it;
                    }
                    for(int i = 0;i < tsize;i++){
                        for(int j = 0;j < tsize;j++){
                            if(i == j)  continue;
                            Node temp;
                            temp.s = tems[i];
                            temp.e = tems[j];
                            temp.num = tsize;
                            vdp[count].push_back(temp);
                        }
                    }
                    kind[count] = vdp[count].size();
                }
                s.clear();
                count++;
            }
            p++;
        }
        //进行dp的过程
        for(int i = 1;i < partnum;i++){
            for(int j = 0;j < kind[i];j++){
                int minx = 10000;
                for(int k = 0;k < kind[i-1];k++){
                    int tem;
                    if(vdp[i][j].s == vdp[i-1][k].e)
                        tem = vdp[i][j].num + vdp[i-1][k].num - 1;
                    else
                        tem = vdp[i][j].num + vdp[i-1][k].num;
                    minx = min(minx,tem);
                }
                vdp[i][j].num  = minx;
            }
        }
        int ans = vdp[partnum-1][0].num;
        for(int i = 1;i < kind[partnum-1];i++)
            ans = min(ans,vdp[partnum-1][i].num);
        cout << ans << endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: