uva11552
将字符串分为len/k块。用dp[i][j]表示第i个块必须以j结尾的最小划分。当第i块没有字符j时,dp[i][j]多计一个。如果当前块只有1种字符,那么就等于dp[i-1][j]。否则对于第i块的所有元素,以它来连接前一块,dp[i][j]=min(dp[i-1][i块中的元素]+i块元素数量-1)。如果第i块中不存在j元素,多计一个。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF,mod=;
int k,len;
char ch[SZ];
int dp[SZ][]; void init()
{
cin>>k>>ch+;
len=strlen(ch+);
int num=len/k;
for(int i=;i<;++i)dp[][i]=;
for(int i=;i<=num;++i)
{
for(int j=;j<;++j)dp[i][j]=INF;
int bg=(i-)*k+,end=i*k;
set<char> st;
for(int j=bg;j<=end;++j)
{
st.insert(ch[j]);
}
if(st.size()==)
{
int cur=*st.begin()-'a';
dp[i][cur]=dp[i-][cur];
for(int j=;j<;++j)
{
if(j!=cur)dp[i][j]=dp[i-][cur]+;
}
}
else
{
for(auto it=st.begin();it!=st.end();++it)
{
int cur=*it-'a';
for(int j=;j<;++j)
{
if(j==cur||st.find(j+'a')==st.end())
{
dp[i][j]=min(dp[i][j],dp[i-][cur]+(int)st.size());
}
else dp[i][j]=min(dp[i][j],dp[i-][cur]+(int)st.size()-);
}
}
}
}
// for(int i=1;i<=num;++i)
// {
// for(int j=0;j<26;++j)
// {
// cout<<dp[i][j]<<" ";
// }cout<<endl;
// }
int minv=*min_element(dp[num],dp[num]+);
cout<<minv<<endl;
} void work()
{ } int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
cin>>casenum;
//cout<<casenum<<endl;
for(lon time=;time<=casenum;++time)
//for(int time=1;cin>>n>>m,n;++time)
{
init();
work();
}
return ;
}
uva11552的更多相关文章
- UVA11552:Fewest Flops
发现如果只有一块就是种类的数目,也就是同种放在一起, 再考虑多块,如果违背的上面的规律,可以发现不会更优, 于是问题就是求在满足同种类放在一起的前提下,尽量使得相邻块的两端一模一样 然后dp一下就可以 ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- UVa 11552 最小的块数(序列划分模型:状态设计)
https://vjudge.net/problem/UVA-11552 题意:输入一个正整数k和字符串S,字符串的长度保证为k的倍数.把S的字符按照从左到右的顺序每k个分成一组,每组之间可以任意重排 ...
随机推荐
- topcoder srm 430 div1
problem1 link 其实就是找到一个数字$t$,使得$x$的二进制为1 的位上$t$也都为1.然后$t$删掉所有那些$x$为1的二进制位就是$k$. problem2 link 设所有合法的边 ...
- 一道cf水题再加两道紫薯题的感悟
. 遇到一个很大的数除以另一个数时,可以尝试把这个很大的数进行,素数因子分解. . 遇到多个数的乘积与另一个数的除法时,求是否能整除,可以先求每一个数与分母的最大公约数,最后若分母数字为1,则证明可整 ...
- SpringBoot 解决HttpServletRequest只能读取一次
业务逻辑,通过filter读取请求的request,获取token,并将token传递后面流程使用 BodyReaderHttpServletRequestWrapper: public class ...
- Linux/shell: Concatenate multiple lines to one line
$ cat file START Unix Linux START Solaris Aix SCO 1. Join the lines following the pattern START with ...
- 用dbms_scheduler创建job
以前一般使用dbms_job来创建job,oracle10g以后推荐使用dbms_scheduler来创建定时任务,dbms_scheduler功能更为强大.一个创建job的例子: begin sys ...
- jquery.validate使用详解
一.简单应用实例: 1.用class样式进行验证,用法简单,但不能自定义错误信息,只能修改jquery-1.4.1.min.js中的内置消息,也不支持高级验证规则. <script type=& ...
- Ural 1297 Palindrome(后缀数组+最长回文子串)
https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...
- python3 session cookie
session是保存在服务器中的,而cookies是保存在客户端中的.服务器通过session id来跟踪客户,而对于客户端而言,session id是保存在cookies中的,所以只要把cookie ...
- 函数指针-如何理解typedef void (*pfun)(void)
问题: 在刚接触typedef void (*pfun)(void) 这个结构的时候,存在疑惑,为什么typedef后只有一"块"东西,而不是两"块"东西呢?那 ...
- new和malloc的用法和区别
从以下几个方面总结下new和malloc的区别: 参考博客: https://blog.csdn.net/nie19940803/article/details/76358673 https://bl ...