UVA - 11552 DP 划分
每k个字符划分一个组,该组内字符顺序可以任意重排,定义块为最长的连续的字符子串,求长度为m*k的字符串中最少的块的数目
设\(dp[i][j]\):前\(i\)组中第\(i\)组结尾为\(j\)的最优解
然后分情况转移即可
吐槽:机子开夜间模式不小心把输出放到循环里(看不见匹配),样例居然过了...
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e4+11;
const double eps = 1e-10;
typedef long long ll;
const int oo = 0x3f3f3f3f;
const double ERR = -2.3333;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int dp[maxn][27];
char str[maxn];
int have[29],kind;
int main(){
int T=read();
while(T--){
int k=read();
s1(str);
memset(dp,0x3f,sizeof dp);
int n=strlen(str+1);
int m=n/k;
rep(i,1,m){
int left=1+(i-1)*k,right=i*k;
kind=0;memset(have,0,sizeof have);
rep(j,left,right){
have[str[j]-'a']++;
if(have[str[j]-'a']==1) kind++;
}
if(i==1) rep(j,0,'z'-'a'){
if(have[j]) dp[1][j]=kind;
}
if(i==1)continue;
rep(j,0,'z'-'a'){
if(!have[j])continue;
rep(k,0,'z'-'a'){
if(j==k){
if(kind==1){
dp[i][j]=min(dp[i][j],dp[i-1][k]);//all j
}else{
dp[i][j]=min(dp[i][j],dp[i-1][k]+kind);
}
}else if(have[k]){
dp[i][j]=min(dp[i][j],dp[i-1][k]+kind-1);
}else{
dp[i][j]=min(dp[i][j],dp[i-1][k]+kind);
}
}
}
}
int ans=oo;
rep(i,0,'z'-'a'){
ans=min(ans,dp[m][i]);
}
println(ans);
}
return 0;
}
UVA - 11552 DP 划分的更多相关文章
- uva 11552 dp
UVA 11552 - Fewest Flops 一个字符串,字符串每 k 个当作一组,组中的字符顺序能够重组.问经过重组后改字符串能够编程最少由多少块字符组成.连续的一段字符被称为块. dp[i][ ...
- UVA 11552 序列划分模型 状态设计DP
这个题目刚看到还真不好下手,把一个是 k的倍数的长度的字符串分成len/k块,每块是k个字母,每个块可以重新组合,最后使得整个序列的相同字母尽量在一起,也就是说,最后会把序列从前往后扫,相连的相同字母 ...
- UVa 11552 DP Fewest Flops
题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...
- 多维DP UVA 11552 Fewest Flop
题目传送门 /* 题意:将子符串分成k组,每组的字符顺序任意,问改变后的字符串最少有多少块 三维DP:可以知道,每一组的最少块是确定的,问题就在于组与组之间可能会合并块,总块数会-1. dp[i][j ...
- uva 11552 Fewest Flops 线性dp
// uva 11552 Fewest Flops // // 二维线性dp // // 首先,在该块必须是相同的来信.首先记录每块有很多种书 // 称为是counts[i]; // // 订购f[i ...
- UVA 11552 四 Fewest Flops
Fewest Flops Time Limit:2000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- codevs1085数字游戏(环形DP+划分DP )
1085 数字游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单, ...
- UVa 11552 Fewest Flops (DP)
题意:给一个字符串,把它分为k块,每一块里面的字母可以任意的排序.最终字符串, 连续的一样的字母算作一个chunk,问总chunks最少是多少? 析:dp[i][j] 表示第 i 个块,第 j 位在末 ...
- UVA 11552 Fewest Flops(区间dp)
一个区间一个区间的考虑,当前区间的决策只和上一次的末尾有关,考虑转移的时候先统计当前区间出现过的字母以及种数ct 枚举上一个区间的末尾标号j,规定小于INF为合法状态,确定j之后看j有没有在当前的区间 ...
随机推荐
- 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
[抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...
- 关闭是否只查看安全传送的网页内容提示框 和 是否允许运行软件,如ActiveX控件和插件提示框
关闭是否只查看安全传送的网页内容提示框 最新编写 爬虫程序,运行程序后,电脑就总是出现下面这个提示框,一遍遍点"是"或"否"繁琐又麻烦.我看得有点不耐烦了.于是 ...
- HDU 3724 Encoded Barcodes (Trie)
题意:给n个字符串,给m个询问,每个询问给k个条形码.每个条形码由8个小码组成,每个小码有相应的宽度,已知一个条形码的宽度只有2种,宽的表示1,窄的表示0.并且宽的宽度是窄的宽度的2倍.由于扫描的时候 ...
- C#中特殊的string类型
string C#有string关键字,在翻 ...
- Jackson Streaming API to read and write JSON
Jackson supports read and write JSON via high-performance Jackson Streaming APIs, or incremental mod ...
- RegExp正则表达式对象
JavaScript的RegExp对象有两种创建方式,一种是字面量,一种是对象. var r = /pattern/attributes或者new RegExp(pattern, attributes ...
- 浅谈短视频APP的发展趋势
2014年6月20日,在AppAnnie最新发布5月应用指数中,美拍荣登“非游戏类iOS榜单”全球下载量第一位置,成为全球iOS应用商店最热门APP.能在<AppAnnie应用指数>这份A ...
- 基于 cookie 的 node 中间层灰度流程的一些思考
此文已由作者申国骏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 关于灰度发布的意义此处就不进行介绍了,可以先读下这两篇文章 <微服务部署:蓝绿部署.滚动部署.灰 ...
- Duration Assertion(持续时间)
Duration Assertion用来测试每一个响应的时间是否小于给定的值,任何超过给定毫秒数的响应都会标记为失败. Duration in milliseconds:响应的持续时间是否在给定的值范 ...
- List_insert
List_insert /* Sorting from little to large use List */ #include <stdio.h> /* printf, scanf, N ...