HDU 5903 Square Distance
$dp$预处理,贪心。
因为$t$串前半部分和后半部分是一样的,所以只要构造前一半就可以了。
因为要求字典序最小,所以肯定是从第一位开始贪心选择,$a,b,c,d,...z$,一个一个尝试过去,如果发现某字符可行,那么该位就选择该字符。
第$i$位选择字符$X$可行的条件:
记这一位选择字符$X$的情况下,对$dis$的贡献为$Q$,$1$至$i-1$位对$dis$贡献和为$F$;
如果第$i+1$位至第$\frac{n}{2}$位,对$dis$的贡献可以凑出$m-Q-F$,那么该位选择$X$可行。
所以可以记$dp[i][j]$表示,第$i$位至第$\frac{n}{2}$位,$dis$为$j$是否可以被凑出,倒着$dp$一下就可以了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
char s[maxn],ans[maxn];
int T,n,m;
int a[maxn],b[maxn];
bool dp[][maxn]; int main()
{
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof dp);
scanf("%d%d",&n,&m); scanf("%s",s); for(int i=;i<=n/;i++) a[i]=s[i-]-'a'+;
for(int i=n/;i<=n-;i++) b[i-n/+]=s[i]-'a'+; dp[n/+][]=;
for(int i=n/;i>=;i--)
{
if(a[i]==b[i])
{
for(int j=;j<=;j++) dp[i][j]=dp[i+][j];
for(int j=;j<=;j++) if(dp[i+][j]==&&j+<=) dp[i][j+]=;
} else
{
for(int j=;j<=;j++)
{
if(dp[i+][j]==)
{
if(j+<=) dp[i][j+]=;
if(j+<=) dp[i][j+]=;
}
}
}
} bool fail=; int z=m;
for(int i=;i<=n/;i++)
{
bool xx=;
for(int j=;j<=;j++)
{
int num=;
if(a[i]!=j) num++; if(b[i]!=j) num++; if(z-num<) continue;
if(dp[i+][z-num])
{
ans[i]=j;
xx=; z=z-num; break;
}
}
if(xx==) fail=;
if(fail==) break;
} if(fail) printf("Impossible\n");
else
{
for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
printf("\n");
}
}
return ;
}
HDU 5903 Square Distance的更多相关文章
- HDU 5903 Square Distance (贪心+DP)
题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...
- hdu 5903 Square Distance(dp)
Problem Description A string is called a square string if it can be obtained by concatenating two co ...
- HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )
题意: 给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s, 要求字典序最小的答案 分析: 把字符串折半,分成0 - n/2-1 和 n/2 - n-1 d ...
- BestCoder Round #87 1002 Square Distance[DP 打印方案]
Square Distance Accepts: 73 Submissions: 598 Time Limit: 4000/2000 MS (Java/Others) Memory Limit ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- Chi Square Distance
The chi squared distance d(x,y) is, as you already know, a distance between two histograms x=[x_1,.. ...
- hdu 5079 Square
http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,… ...
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
随机推荐
- iOS推送服务细节回顾
iOS推送服务细节回顾 之前在做推送功能时候,就总结过一系列证书的制作,OC代码实现和服务器搭建等经验.又过了一段时间了,前前后后对推送服务做了多次的完善和优化,有iOS客户端的,还有本地服务器端的. ...
- c# 服务程序重启自身
由于工作有这样的需求,于是各种寻找,但是都不满意,偶然间想起一个方法 1.修改服务的Program.cs文件如下 static void Main(string[] args) { //LoadFil ...
- facebook api介绍
转自(http://sls.weco.net/node/10773) 一.Facebook API 基礎概念 Facebook API 概論 : API 最大的好處在於可以讓程式開發人員只需要根據 A ...
- Paint.NET
http://www.dotpdn.com/downloads/pdn.html Paint.NET To download, please click the Free Download Now l ...
- - 高级篇:二,IL设置静态属性,字段和类型转换
- 高级篇:二,IL设置静态属性,字段和类型转换 静态属性赋值 先来看 Reflector反射出的IL源码(感谢Moen的提示),这次用 Release模式编译,去掉那些无用的辅助指令 public ...
- [Ext JS 4] contentEL,renderTo, applyTo 释义与区别
前言 若干年前,使用Ext JS 3 开发了一个系统. 随着Ext JS 4的出现,总是会看到或听到关于Ext 比较多的言论是 : Ext JS 4 较Ext JS 3 有较大的改变. Ext JS ...
- jquery跨域请求数据
jquery跨域请求数据 jquery跨越请求数据.实际开发中经常会碰到两个网站数据交互问题,当向另一个站点请求数据该如何做? 实际上非常容易,请按照下面的步骤做: 第一:编写js,通过get获取远程 ...
- TCPDump 抓Loopback数据包
编写网络程序必备截包工具, unix下面自带tcpdump, linux就不用说了.用于排查网络程序的bug,命令行如何使用请百度谷歌.分析包推荐wireshark,可视化非常方便.一般都是在非Win ...
- 从struts2拦截器到自定义拦截器
拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.xml的配置中,package内直接或间接继承了struts-default.xml,这样st ...
- 飘逸的python - 简明gzip模块压缩教程
压缩数据创建gzip文件 先看一个略麻烦的做法 import StringIO,gzip content = 'Life is short.I use python' zbuf = StringIO. ...