题意:给一个字符串,把它分为k块,每一块里面的字母可以任意的排序。最终字符串, 连续的一样的字母算作一个chunk,问总chunks最少是多少?

析:dp[i][j] 表示第 i 个块,第 j 位在末尾时chunk最少,状态转移方程也应该好写,如果 dp[i-1][j] 和第 i 块第一个一样,那么就总数就会-1,

否则就是直接加上。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1LL << 60;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn][maxn];
char s[maxn];
int a[maxn];
bool vis[30]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
int cnt = 0;
scanf("%s", s);
int len = strlen(s);
int t = len / n;
memset(dp, INF, sizeof dp);
memset(vis, false, sizeof vis);
for(int i = 0; i < n; ++i)
vis[s[i]-'a'] = true;
for(int i = 0; i < 26; ++i) if(vis[i]) ++cnt;
for(int i = 0; i < n; ++i) dp[0][i] = cnt; for(int i = 1; i < t; ++i){
cnt = 0;
memset(vis, false, sizeof vis);
for(int j = i*n; j < (i+1)*n; ++j)
vis[s[j]-'a'] = true;
for(int j = 0; j < 26; ++j) if(vis[j]) ++cnt; for(int j = 0; j < n; ++j){
int fro = i * n + j;
for(int k = 0; k < n; ++k){
int rear = (i-1) * n + k;
if(vis[s[rear]-'a'] && (1 == cnt || s[rear] != s[fro])) dp[i][j] = min(dp[i][j], dp[i-1][k] + cnt - 1);
else dp[i][j] = min(dp[i][j], dp[i-1][k] + cnt);
}
}
}
int ans = INF;
for(int i = 0; i < n; ++i) ans = min(ans, dp[t-1][i]);
printf("%d\n", ans);
}
return 0;
}

UVa 11552 Fewest Flops (DP)的更多相关文章

  1. uva 11552 Fewest Flops 线性dp

    // uva 11552 Fewest Flops // // 二维线性dp // // 首先,在该块必须是相同的来信.首先记录每块有很多种书 // 称为是counts[i]; // // 订购f[i ...

  2. UVA 11552 Fewest Flops(区间dp)

    一个区间一个区间的考虑,当前区间的决策只和上一次的末尾有关,考虑转移的时候先统计当前区间出现过的字母以及种数ct 枚举上一个区间的末尾标号j,规定小于INF为合法状态,确定j之后看j有没有在当前的区间 ...

  3. UVA - 11552 Fewest Flops

    传送门: 题目大意:给你一个字符串,可以平均分成很多段,每一段之内的元素可以任意排序,最后再按原来的顺序把每一段拼起来,问最少的块数.(块:连续相同的一段字符成为一个块) 题解: 首先我们可以发现,每 ...

  4. 多维DP UVA 11552 Fewest Flop

    题目传送门 /* 题意:将子符串分成k组,每组的字符顺序任意,问改变后的字符串最少有多少块 三维DP:可以知道,每一组的最少块是确定的,问题就在于组与组之间可能会合并块,总块数会-1. dp[i][j ...

  5. uva 11552 dp

    UVA 11552 - Fewest Flops 一个字符串,字符串每 k 个当作一组,组中的字符顺序能够重组.问经过重组后改字符串能够编程最少由多少块字符组成.连续的一段字符被称为块. dp[i][ ...

  6. UVA 11552 四 Fewest Flops

    Fewest Flops Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  7. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  8. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

  9. UVa 11552 DP Fewest Flops

    题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

随机推荐

  1. springcloud微服务实战--笔记--1、基础知识

    微服务的问题: 分布式事务和数据一致性. 由于分布式事务本身第实现难度就非常大,所以在微服务架构中,我们更强调在各服务之间进行无事务第调用,而对于数据一致性,只要求数据在最后第处理状态是一致第即可:若 ...

  2. poj 3071 Football <DP>

    链接:http://poj.org/problem?id=3071 题意: 有 2^n 支足球队,编号 1~2^n,现在给出每支球队打败其他球队的概率,问哪只球队取得冠军的概率最大? 思路: 设dp[ ...

  3. vue element-ui 自动获取光标

    <el-input ref="customerInput"></el-input> this.$refs.mark.$el.querySelector('i ...

  4. UITableViewCell的多选操作

    版权声明:本文为博主原创文章.未经博主同意不得转载,转载需加上原博客链接. https://blog.csdn.net/panyong4627/article/details/37902207 - ( ...

  5. Java基础之Comparable接口, Collections类,Iterator接口,泛型(Generic)

    一.Comparable接口, Collections类 List的常用算法: sort(List); 排序,如果需要对自定义的类进行排序, 那就必须要让其实现Comparable接口, 实现比较两个 ...

  6. Bootstrap——组件

    1.字体图标 <span class="glyphicon glyphicon-star" aria-hidden="true"></span ...

  7. Ruby JSON操作

      解析来我们就可以使用以下命令来安装Ruby JSON 模块: ? 1 $gem install json 使用 Ruby 解析 JSON 以下为JSON数据,将该数据存储在 input.json ...

  8. LightOJ - 1038 Race to 1 Again —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1038 1038 - Race to 1 Again    PDF (English) Statistics Foru ...

  9. ActivemMQ之消息服务器平台(发邮件)

    消息服务平台 处理公司内部各种消息业务 比如 发送邮件  发送短信  微信推送 接口有两种类型 异步 同步 同步需求: 当调用消息服务平台,需要返回消息服务平台调用第三方平台接口是否成功 异步需求: ...

  10. CentOS 7 设置自定义开机启动,添加自定义系统服务

    详细文档,http://www.linuxidc.com/Linux/2015-04/115937.htm 摘自: http://www.centoscn.com/CentOS/config/2015 ...