题意:给一个字符串,把它分为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. vue 后台交互数据-编辑页面

    思路~点击编辑按钮,需要获取当前列的id,然后根据id请求后台当前id的数据 1.~~ 2.接收id,并请求页面 获取数据

  2. Java for LeetCode 101 Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. 更新TP-LINK路由器的外网IP到花生壳动态IP解析

    ------------------------------------------------------------------------------- 以下内容可能还是存在问题,等之后有时间再 ...

  4. Android中m、mm、mmm、mma、mmma的区别

    m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...

  5. 常见的CSS命名

    1:header(头部)logo  topbar lang search topmenu banner nav headbox active(活动的) selectselectTop selectLi ...

  6. The Contiki build system

    The Contiki build system http://contiki.sourceforge.net/docs/2.6/a01796.html 先看官方文档的说明,对contiki的构建系统 ...

  7. 解决ini-parser解析ini文件中文乱码问题

    rickyah/ini-parser 是一个.net 平台解析ini文件的库,当ini文件中含有中文字符时会乱码. 解决:将文件通过Editplus 等文本编辑工具保存为 utf-8 + bom 格式 ...

  8. Win7系统中用anaconda配置tensorflow运行环境

    前言:anaconda是一个python Data Science Platform.安装它的契机是因为要用tensorflow. 安装完后感觉用它来管理python运行环境还是挺方便的,常用的con ...

  9. composer镜像安装laravel

    博主最近在学习Laravel的框架的相关知识,对于Laravel的许多新特性,大家最好还是去查看官网文档最好,Laravel的文档非常完善,中文英文的都有,可以很好的解决你的困惑. 但是我们会发现学习 ...

  10. [POI 2000] 公共串

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2946 [算法] 建立多串后缀树 对于后缀树上的每个点 , 判断该节点所代表的等价类是 ...