UVA 11552 Fewest Flops
2015-11-24 11:56
260 查看
水题
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define MAX 1100 using namespace std; char s[MAX]; int L,k,dp[MAX][26],count_t[26]; void init(){ cin>>k; cin>>s; L=strlen(s); memset(dp,0x7f,sizeof(dp)); } void solve(){ for(int i=0;i<L/k;i++){ int front=i*k,rear=(i+1)*k-1; memset(count_t,0,sizeof(count_t)); for(int j=front;j<=rear;j++) count_t[s[j]-'a']++; int kind=0; for(int j=0;j<26;j++) if(count_t[j]) kind++; for(int j=0;j<26;j++){ if(count_t[j]){ if(i==0) dp[i][j]=kind; else{ for(int t=0;t<26;t++){ if(j!=t){ if(count_t[t]) dp[i][j]=min(dp[i][j],dp[i-1][t]+kind-1); else dp[i][j]=min(dp[i][j],dp[i-1][t]+kind); } else{ if(kind==1) dp[i][j]=min(dp[i][j],dp[i-1][t]); else dp[i][j]=min(dp[i][j],dp[i-1][t]+kind); } } } } } } int ans=0x7f7f7f7f; for(int i=0;i<26;i++){ ans=min(ans,dp[L/k-1][i]); } cout<<ans<<endl; } int main(){ int T; cin>>T; while(T--){ init(); solve(); } return 0; }
相关文章推荐
- firebug更新1.12,新功能和改进解析
- jQuery $.ajax写法-自用
- 基于jquery实现日历签到功能
- CSS box-flex属性,然后弹性盒子模型简介
- Jquery1.9.1源码分析系列(六)延时对象应用之jQuery.ready
- css background属性的使用方法
- JSON及JSON-LIB
- jQuery源码学习7——实例成员
- Javascript:ajax
- html 网页代码大全,总结,使用
- 【Copy】jQuery常用方法一览
- C#实现将javascript文件编译成dll文件的方法
- HTML中<script ...>....</script>标签的正则表达式?
- JS实现图片高亮展示效果实例
- JQuery按照form上传文件+asp.net后台处理(三)
- 认识CSS的权重
- React:组件的生命周期
- jquery autocomplete 自动提示实例
- 关于string.substring() string.slice() splice()方法 在js和c#中的不同
- EF6+ MVC Insert or update pattern