Square Distance (构造)

本文介绍了一个算法问题,目标是在给定字符串和特定哈明距离条件下找到字典序最小的平方字符串。文章详细解释了问题背景、输入输出要求,并提供了一段示例代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Square Distance

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 312    Accepted Submission(s): 102


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 distance between two strings of equal length is the number of positions at which the corresponding symbols are different.

Peter has a string s=s1s2...sn of even length. He wants to find a lexicographically smallest square string t=t1t2...tn that the hamming distance between s and tis exact m. In addition, both s and t should consist only of lowercase English letters.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (1n1000,0mn,n is even) -- the length of the string and the hamming distance. The second line contains the string s.
 

Output
For each test case, if there is no such square string, output "Impossible" (without the quotes). Otherwise, output the lexicographically smallest square string.
 

Sample Input
#
3 4 1 abcd 4 2 abcd 4 2 abab
 

Sample Output
Impossible abab aaaa
 


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
const int mod = 2520;


int gcd(int a, int b)
{
    while(b)
    {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}


int T,n,m;


int sum[550];


char st[1200];
char re[1200];


int main()
{
    scanf("%d",&T);


    while(T--)
    {
        scanf("%d%d",&n,&m);


        scanf("%s",&st);


        int tt=0;
        memset(sum,0,sizeof(sum));


        for(int i=0;i<n/2;i++)
        {
            if(st[i]!=st[i+n/2]) tt++;


            sum[i] = tt;
        }


        if(tt>m) {printf("Impossible\n");continue;}


        int ff=0;
        bool flag=true;
        for(int i=0;i<n/2;i++)
        {
            int tmp=0,uu,oo;


            re[i] = 'a';


            if(re[i]!=st[i]) tmp++;
            if(re[i]!=st[i+n/2]) tmp++;


            uu=ff+tmp+sum[n/2-1] - sum[i];
            oo=ff+tmp+ (n/2-1 - i)*2;
            if(uu<=m&&oo>=m)
            {
                ff+=tmp;
                continue;
            }


            tmp=0;
            re[i] = 'b';


            if(re[i]!=st[i]) tmp++;
            if(re[i]!=st[i+n/2]) tmp++;


            uu=ff+tmp+sum[n/2-1] - sum[i];
            oo=ff+tmp+ (n/2-1 - i)*2;
            if(uu<=m&&oo>=m)
            {
                ff+=tmp;
                continue;
            }


            tmp=0;
            re[i] = 'c';


            if(re[i]!=st[i]) tmp++;
            if(re[i]!=st[i+n/2]) tmp++;


            uu=ff+tmp+sum[n/2-1] - sum[i];
            oo=ff+tmp+ (n/2-1 - i)*2;
            if(uu<=m&&oo>=m)
            {
                ff+=tmp;
                continue;
            }


            tmp=0;
            re[i] =min(st[i],st[i+n/2]);


            if(re[i]!=st[i]) tmp++;
            if(re[i]!=st[i+n/2]) tmp++;


            uu=ff+tmp+sum[n/2-1] - sum[i];
            oo=ff+tmp+ (n/2-1 - i)*2;
            if(uu<=m&&oo>=m)
            {
                ff+=tmp;
                continue;
            }


            flag=false;
           // printf("hkfghfkhg\n");
        }


        if(flag==false)
        {
            printf("Impossible\n");continue;
        }


        for(int i=0;i<n/2;i++)
        {
            printf("%c",re[i]);
        }
         for(int i=0;i<n/2;i++)
        {
            printf("%c",re[i]);
        }
        printf("\n");




    }


    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值