uva 11552 Fewest Flops dp+贪心

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
题意:
给出一个字符串和一个数k
每段小字符串长度为k
第一段是0-k-1
第二段是k-2*k-1
依次类推
每段小字符里的字母都能互换(但是只能与本段的字母交换)
然后恢复成大字符串
在读的时候如果有连续的相同的字母 则只读一遍(例aabbcc读三遍abcabc读六遍)
问经过互换最少读几次
唉尼玛语文太差题目都描述不清楚- -
贴代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
    int dp[1111][27];
    int cnt[1111];
    bool vis[1111][27];
    char str[1111];
    int k,i,j,l,kk;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%s",&k,str);
        l=strlen(str);
        memset(dp,INF,sizeof(dp));
        memset(cnt,0,sizeof(cnt));
        memset(vis,0,sizeof(vis));
        for(i=0;i<l;i+=k)
        {
            for(j=i;j<i+k;j++)
            {
                if(!vis[i][str[j]-'a'])
                    cnt[i]++;
                vis[i][str[j]-'a']++;
            }
        }
        int ans=INF;
        if(l==k)
            ans=cnt[0];
        else
        {
            for(i=0;i<26;i++)
                if(vis[0][i])
                    dp[0][i]=cnt[0];
            for(i=k;i<l;i+=k)
            {
                for(j=0;j<26;j++)
                {
                    if(vis[i-k][j])
                    {
                        for(kk=0;kk<26;kk++)
                        {
                            if(!vis[i][kk])
                                continue;
                            if(vis[i][j]&&(cnt[i]==1||j!=kk))//以j开头 kk结尾 若cnt[i]!=1则j不能等于kk
                                dp[i][kk]=min(dp[i][kk],dp[i-k][j]+cnt[i]-1);
                            else
                                dp[i][kk]=min(dp[i][kk],dp[i-k][j]+cnt[i]);
                            if(i==l-k)
                                ans=min(ans,dp[i][kk]);
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值