HDU 5903 Square Distance (贪心+DP)

题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", "abba"不是. 两个长度相同字符串之间的

hamming distance是对应位置上字符不同的位数. 给定一行字符串和 m,输出字典序最小的字符串。

析:首先先用dp判断能不能形成这样的字符串,然后再打印出来,dp[i][j] 表示 i - 中间的数能不能改 j 个字符得到,最后打印时贪心,从最小的开始,如果能用结束。

代码如下:

#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 <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
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 int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn/2][maxn];
char s[maxn], ans[maxn];

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%d %d", &n, &m);
        scanf("%s", s+1);
        memset(dp, false, sizeof dp);

        int mid = n/2;
        dp[mid+1][0] = true;
        for(int i = mid; i >= 1; --i){
            if(s[i] == s[n-mid+i]){
                for(int j = 0; j <= m; ++j){
                    if(j > 1)  dp[i][j] |= dp[i+1][j-2];
                    dp[i][j] |= dp[i+1][j];
                }
            }
            else{
                for(int j = 0; j <= m; ++j){
                    if(j)  dp[i][j] |= dp[i+1][j-1];
                    if(j > 1) dp[i][j] |= dp[i+1][j-2];
                }
            }
        }

        if(!dp[1][m])  puts("Impossible");
        else{
            int cnt = m;
            for(int i = 1; i <= mid; ++i){
                for(int j = 0; j < 26; ++j){
                    if(s[i] == ‘a‘+j || s[i+mid] == ‘a‘+j){
                        if(s[i] == s[i+mid]){
                            if(dp[i+1][cnt]) {  ans[i] = ans[i+mid] = s[i]; break; }
                        }
                        else{
                            if(dp[i+1][cnt-1]){ ans[i] = ans[i+mid] = ‘a‘ + j;  --cnt;  break; }
                        }
                    }
                    else{
                        if(dp[i+1][cnt-2]){ ans[i] = ans[i+mid] = ‘a‘ + j;  cnt -= 2;  break; }
                    }
                }
            }
            ans[n+1] = 0;
            puts(ans+1);
        }
    }
    return 0;
}
时间: 2024-07-30 10:17:44

HDU 5903 Square Distance (贪心+DP)的相关文章

hdu 5903 Square Distance(dp)

Problem Description A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Hamming distanc

HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )

题意: 给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s, 要求字典序最小的答案    分析: dp[i][j] 表示 第i位及之后的总代价为j可不可行 从第 n/2-1 位推回第 0 位, 若dp[0][m] = 1,则存在 然后贪心对每一位从'a'试到'z',选取接下来存在解的字符 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #inclu

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$可

HDU 2376 Average distance (树形dp)

Average distance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 588    Accepted Submission(s): 213 Special Judge Problem Description Given a tree, calculate the average distance between two ve

HDU 4362 Dragon Ball 贪心DP

Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball wil

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

BestCoder Round #87 1002 Square Distance[DP 打印方案]

Square Distance Accepts: 73 Submissions: 598 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", "abba"不是. 两个长度相同字

HDU 4939 Stupid Tower Defense(贪心+dp)

HDU Stupid Tower Defense 题目链接 题意:有一些塔,红塔能攻击经过他的,绿塔能攻击经过之后的,蓝塔能把经过之后的减速,求在1-n上放塔,求伤害最大值 思路:一开始以为直接贪心,绿塔最前,蓝塔中间,红塔最后就可以了,结果其实是错的 不过,红塔放最后是肯定的,这个很显然就不多证明了,是贪心的思想 然后就dp[i][j]表示放到i,前面有j个绿塔去状态转移即可 代码: #include <cstdio> #include <cstring> #include &l

HDU 4939 Stupid Tower Defense(dp+贪心)

dp[i][j]表示到了第i步放了j个减速,造成的伤害.我们用贪心的策略把造成一段伤害的放在最后面,造成持续伤害的与减速放在前i个中这样得到的伤害是最高的. 所以前(i,j)中的伤害为dp[i][j] = max(dp[i-1][j]+(j*z+t)*(max(0LL, i-1-j))*y, dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y); 每次造成的伤害就为:dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y).然后取一个最大值,就是伤害的最大值了. Stu