题意:给一个字符串,把它分为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;
}

最新文章

  1. 编写中断例程7ch:计算word型数据的平方
  2. Codeforces Round# 305 (Div 1)
  3. [Android] ImageView.ScaleType设置图解 【转载】
  4. 新浪短链接API接口示例
  5. 创建采购订单批到程序用的BAPI
  6. ReOut
  7. 控制结构(2) 卫语句(guard clause)
  8. maven库
  9. Linux信号实践(3) --信号内核表示
  10. vscode 添加 includePath
  11. Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明
  12. TypeScipt学习
  13. python系统编程(十一)
  14. 【PMP】项目风险管理~重点知识
  15. What does -&gt; do in clojure?
  16. iOS - 系统权限(关键时刻很有用的)
  17. git的使用(本地及关联远程,上传到远程)
  18. VS2008+Windows DDK 7的环境配置(二)
  19. [RGEOS]数学基础
  20. angular based app开发流程

热门文章

  1. Angular入门(二) 服务
  2. Netty Bootstrap(图解)|秒懂
  3. Jeff Dean 排序时间计算
  4. python 搜集参数的共有项和所有项
  5. apache 绿色版 安装
  6. imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。
  7. me12里更改信息记录的净价和有效价格,以及信息记录的条件价格
  8. 【python】使用python写windows服务
  9. Matlab图像处理(03)-基本概念
  10. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列