UVA 11552 Fewest Flops 多维dp

 Fewest Flops
Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

Download as PDF

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 kS2 be the substring of S from + 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 be “uuvuwwuv” and be 4. Then S1 is “uuvu” and has three chunks, but may be rearranged to “uuuv” which has two chunks. Similarly, Smay 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 (1 ≤ ≤ 100), the number of test cases. The following lines contain an integer 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

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int INF=11111;//千万别把INF开太大。。惨痛的教训啊,两个小时。。
const int maxn=1111;

char s[maxn];
int f[maxn][maxn];
bool vis[maxn];


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j,m;
        int k,res;
        scanf("%d%s",&k,s);
        int len=strlen(s);
        memset(f,INF,sizeof(f));
        for(i=1;i<=len/k;i++)
        {
            memset(vis,false,sizeof(vis));
            for(j=1;j<=k;j++)
            {
                vis[s[(i-1)*k+j-1]]=true;
            }
            res=0;
            for(j='a';j<='z';j++)
                if(vis[j])
                res++;
            if(i==1)
            {
                for(j=1;j<=k;j++)
                    f[1][j]=res;
                continue;
            }
            for(j=1;j<=k;j++)
            {
                int p=(i-1)*k+j-1;
                for(m=1;m<=k;m++)
                {
                    int q=(i-2)*k+m-1;
                    if( vis[s[q]] && ( res==1 || s[p]!=s[q] ))
                        f[i][j]=min(f[i][j],f[i-1][m]+res-1);
                    else
                        f[i][j]=min(f[i][j],f[i-1][m]+res);
                }
            }
        }
        int ans=INF;
        for(i=1;i<=k;i++)
            ans=min(ans,f[len/k][i]);
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值