uva 11552 Fewest Flops 线性dp
// uva 11552 Fewest Flops
//
// 二维线性dp
//
// 首先,在该块必须是相同的来信。首先记录每块有很多种书
// 称为是counts[i];
//
// 订购f[i][j]它代表前i字母j为结尾的最小分块数
//
// 假设第i块的開始字母与第i-1块的结束字母同样
// f[i][j] = min(f[i][j],f[i-1][k] + counts[i] - 1);
//
// 否则
//
// f[i][j] = min(f[i][j],f[i-1][k] + counts[i]);
//
// 第一种情况的開始和结束字母同样有个前提:
// f[i-1][k]中的k在第i块中出现
//
// 之后的条件是:
// 1)第i块仅仅有一种字符
// 2)第i块结束字符与第i-1块结束字符不同样
//
// ps:第一种情况是另外一种情况的特殊,由于每种字母要么是在開始,
// 要么是在结束,不会连续的两个块结束字母同样,这样肯定不是
// 最优的,而假设第i块仅仅有一种字符。那么显然不在这之列。
//
// wrong answer了10次。如此顽强我也是醉了
//
// 继续练吧
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L); const int maxn = 1008;
const int inf = 0x3f3f3f3f;
char s[maxn];
bool vis[maxn][30];
int f[maxn][300];
int n,k;
int counts[maxn]; void dp(){
for (int i=0;i<=26;i++){
if (vis[1][i]){
f[1][i] = counts[1];
}
} for (int i=2;i<=n/k;i++){
for (int j=0;j<26;j++){
if (vis[i][j]){
for (int m=0;m<26;m++){
if (vis[i][m] && (counts[i]==1 || j!=m)){
f[i][j] = min(f[i][j],f[i-1][m] + counts[i] - 1);
}else{
f[i][j] = min(f[i][j],f[i-1][m] + counts[i]);
}
}
}
}
}
int ans = inf;
for (int i=0;i<26;i++){
ans = min(ans,f[n/k][i]);
}
printf("%d\n",ans); }
void print(){
for (int i=1;i<=n/k;i++){
cout << counts[i] << " ";
}
cout << endl;
}
void init(){
scanf("%d %s",&k,s+1);
n = strlen(s+1);
memset(vis,0,sizeof(vis));
memset(f,inf,sizeof(f));
memset(counts,0,sizeof(counts));
int x=1;
for (int i=1;i<=n;i++){
if (!vis[x][s[i]-'a']){
counts[x]++;
vis[x][s[i]-'a'] = 1;
}
if (i%k==0){
x++;
}
}
// cout << "x = " << x << endl;
// print();
dp();
} int main() {
int t;
//freopen("E:\\Code\\1.txt","r",stdin);
scanf("%d",&t);
while(t--){
init();
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
uva 11552 Fewest Flops 线性dp的更多相关文章
- UVa 11552 Fewest Flops (DP)
题意:给一个字符串,把它分为k块,每一块里面的字母可以任意的排序.最终字符串, 连续的一样的字母算作一个chunk,问总chunks最少是多少? 析:dp[i][j] 表示第 i 个块,第 j 位在末 ...
- UVA 11552 Fewest Flops(区间dp)
一个区间一个区间的考虑,当前区间的决策只和上一次的末尾有关,考虑转移的时候先统计当前区间出现过的字母以及种数ct 枚举上一个区间的末尾标号j,规定小于INF为合法状态,确定j之后看j有没有在当前的区间 ...
- UVA - 11552 Fewest Flops
传送门: 题目大意:给你一个字符串,可以平均分成很多段,每一段之内的元素可以任意排序,最后再按原来的顺序把每一段拼起来,问最少的块数.(块:连续相同的一段字符成为一个块) 题解: 首先我们可以发现,每 ...
- 多维DP UVA 11552 Fewest Flop
题目传送门 /* 题意:将子符串分成k组,每组的字符顺序任意,问改变后的字符串最少有多少块 三维DP:可以知道,每一组的最少块是确定的,问题就在于组与组之间可能会合并块,总块数会-1. dp[i][j ...
- uva 11552 dp
UVA 11552 - Fewest Flops 一个字符串,字符串每 k 个当作一组,组中的字符顺序能够重组.问经过重组后改字符串能够编程最少由多少块字符组成.连续的一段字符被称为块. dp[i][ ...
- UVA 11552 四 Fewest Flops
Fewest Flops Time Limit:2000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- Hadoop读书笔记(一)Hadoop介绍
1.Hadoop是什么? 适合大数据的分布式与计算平台. HDFS:Hadoop Distributed File System 分布式文件系统 MapReduce:并行计算框架 2.HDFS的架构 ...
- php课程 8-30 实现验证码验证的难点是什么
php课程 8-30 实现验证码验证的难点是什么 一.总结 一句话总结:session技术实现验证码传递. 1.生成验证码的那个网页(php文件)中的验证码怎么搁到别的网页中去? 直接在img的src ...
- php课程 1-3 web项目中php、html、js代码的执行顺序是怎样的(详解)
php课程 1-3 web项目中php.html.js代码的执行顺序是怎样的(详解) 一.总结 一句话总结:b/s结构 总是先执行服务器端的先.js是客户端脚本 ,是最后执行的.所以肯定是php先执行 ...
- 算法 Tricks(四)—— 判断序列中的字符/数值是否交替出现
比如:353, 54545,数字都是交替出现的: bool alternate = true; for (int i = 0; i < M.size(); ++i){ if (M[i] != M ...
- MySQL数据导出导入任务脚本
#!/usr/bin/env python#-*- encoding: utf8 -*- import timeimport osimport mysql.connector #定义一些全局变量 w ...
- Android——WebView方式开发web App
昨天接到个酬劳丰厚的任务.把java新生系统做成webApp,想想蛮简单的.所以当时就装作非常为难的样子答应了. 所谓Web App,用曾经我那个老PM的话来说.就是在壳里面套上页面.这里的壳相当于浏 ...
- form表单嵌套,用标签的form属性来解决表单嵌套的问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- JavaScript 正則表達式
一.简单介绍 1.什么是正則表達式 正則表達式本身就是一种语言,这在其他语言是通用的. 正則表達式(regular expression)描写叙述了一种字符串匹配的模式,能够用来检查一个串是否含有某种 ...
- [Angular] FormBuildAPI
Using FormBuilder API can simply our code, for example we want to refactor following code by using F ...