UVA 11552——Fewest Flops
来源:互联网 发布:中国家具出口数据 编辑:程序博客网 时间:2024/06/03 19:37
题意:给定一个长为s的字符串(其中s为k的倍数),然后按照前后分成s/k 组,每组之内可以重排,使得重排后的块数最少(一段连续的字母算是一块)。
思路:区间dp,dp[i][fa]表示前i组且第i+1组的最左边的字符是fa+'a'的状态能得到的最少的块数,那么状态转移也就是枚举两边应该放什么字符。
code:
#include <bits/stdc++.h>using namespace std;const int N=1005;vector<int>g[N];int n,k,dp[N][26];char s[N];int dfs(int i,int j){ if (dp[i][j]>=0) return dp[i][j]; if (i==0) return 0; vector<int>& v=g[i]; int ans=INT_MAX; for (int k=0;k<v.size();k++){ int cnt=v.size(); if (v[k]==j) cnt--; for (int p=0;p<v.size();p++) ans=min(ans,dfs(i-1,v[p])+cnt+(p==k&&v.size()>1?1:0)); } return dp[i][j]=ans;}int main(){ int T; scanf("%d",&T); while (T--){ scanf("%d%s",&k,&s);n=0; for (int i=0;s[i]!='\0';i+=k){ char s1[N]; for (int j=i;j<i+k;j++) s1[j-i]=s[j];s1[k]='\0'; sort(s1,s1+k); g[++n].clear(); for (int j=0;j<k;j++) if (s1[j]!=s1[j+1]) g[n].push_back(s1[j]-'a'); } memset(dp,-1,sizeof(dp)); printf("%d\n",dfs(n,26)); }}
0 0
- UVA 11552——Fewest Flops
- UVA 11552 Fewest Flops
- uva 11552 Fewest Flops
- UVA 11552 Fewest Flops
- UVA 11552 Fewest Flops
- UVA - 11552 Fewest Flops
- UVa:11552 - Fewest Flops
- UVA 11552 Fewest Flops
- uva 11552 Fewest Flops
- UVA 11552 Fewest Flops
- Uva 11552 Fewest Flops
- UVA 11552 Fewest Flops
- uva 11552 - Fewest Flops(dp)
- UVA 11552 Fewest Flops(DP)
- FEWEST FLOPS - UVa 11552 dp
- UVA - 11552 Fewest Flops DP
- uva 11552 - Fewest Flops(dp)
- Uva 11552 Fewest Flops (三维动态规划)
- 【poj 2627】 Sudoku 题意&题解&代码(C++)
- 【iOS开发-103】解决方案:iOS8.1中UIBarButtonItem的setTitleTextAttributes对Disabled颜色设置无效
- sql约束
- 线程处理(C# 编程指南)学习笔记1
- Cocos动画
- UVA 11552——Fewest Flops
- ios中的反射
- git-am 和 format-patch 的使用
- struts2 文件上传
- linux常用命令
- Cocos骨骼动画
- mac端如何快速搭建一个web服务器
- 码农小汪-Hibernate学习4-关联关系映射many-to-one,many-to-one,one-to-many
- 前台页面总结