首页 > 其他分享 >hdoj-5903 Square Distance

hdoj-5903 Square Distance

时间:2023-06-12 17:36:58浏览次数:58  
标签:Distance Square string int 5903 square str include dp


原题链接

Square Distance


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



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


 




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 (1≤n≤1000,0≤m≤n,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



dp[i][j]表示第i个字符到n/2个字符以及i+n/2到n个字符与原字符串之间的距离是否能为j


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 100005
using namespace std;
typedef long long ll;

char str[1005];
int dp[505][1005];
int main(){
	
	//freopen("in.txt", "r", stdin);
	int t;
	
	scanf("%d", &t);
	while(t--){
		int n, m;
		scanf("%d%d%s", &n, &m, str+1);
		memset(dp, 0, sizeof(dp));
		dp[n/2+1][0] = 1;
		for(int i = n/2; i > 0; i--){
			if(str[i] == str[i+n/2]){
				for(int j = 0; j <= m; j++)
				 dp[i][j] = dp[i+1][j];
				for(int j = 0; j <= m-2; j++){
					if(dp[i+1][j])
					 dp[i][j+2] = 1;
				}
			}
			else{
				for(int j = 0; j <= m-1; j++)
				 dp[i][j+1] = dp[i+1][j];
				for(int j = 0; j <= m-2; j++)
				 if(dp[i+1][j])
				  dp[i][j+2] = 1;
			}
		}
		if(dp[1][m] == 0)
		 puts("Impossible");
		else{
			int k = m;
			for(int i = 1; i <= n/2; i++){
				for(int j = 0; j <= 25; j++){
					int p = 0;
					if(str[i] != j + 'a')
					  p++;
					if(str[i+n/2] != j + 'a')
					 p++;
			       
					if(dp[i+1][k-p]){
						str[i] = str[i+n/2] = j + 'a';
						k -= p;
						break;
					}
				}
			}
			puts(str+1);
		}
	}
	return 0;
}





标签:Distance,Square,string,int,5903,square,str,include,dp
From: https://blog.51cto.com/u_16158872/6464259

相关文章

  • Stanford NLP第三课“最小编辑距离(Minimum Edit Distance)”
    一、课程介绍斯坦福大学于2012年3月在Coursera启动了在线自然语言处理课程,由NLP领域大牛DanJurafsky和ChirsManning教授授课:链接地址以下是本课程的学习笔记,以课程PPT/PDF为主,其他参考资料为辅,融入个人拓展、注解,抛砖引玉,欢迎大家在“我爱公开课”上一起探讨学习。课件汇总下载......
  • Codeforces 1495F - Squares
    不知道怎么放到div1F的,感觉没啥亮点。首先对于一条\(1\)到\(n+1\)的路径而言,它经过的点的编号一定是递增的,也就是说,如果我们将关键点大小排个序,那么答案就是相邻两点间最短路的和。删/加点造成的变化是\(O(1)\)的,所以问题等价于,多次询问这张图中\(x,y\)之间最短路的......
  • [ABC201E] Xor Distances 题解
    XorDistances题目大意给定一颗带边权无根树,定义\(\text{dis}(i,j)\)表示\(i,j\)两点在树上的最短路径的边权的异或和。求:\[\sum_{i=1}^n\sum_{j=i+1}^n\text{dis}(i,j)\]思路分析首先,容易证明:\[\text{dis}(i,j)=\text{dis}(i,x)\oplus\text{dis}(x,j)\]这个式子告诉我......
  • leetcode 461. Hamming Distance
    The Hammingdistance betweentwointegersisthenumberofpositionsatwhichthecorrespondingbitsaredifferent.Giventwointegers x and y,calculatetheHammingdistance.Note:0≤ x, y <231.Example:Input:x=1,y=4Output:2Explanation:1......
  • LeetCode-Java题解 977. Squares of a Sorted Array
    题目地址:977.SquaresofaSortedArray解题思路:    又是一道双指针的题目,看见秒想到双指针(平方直接调用sort方法也行,但是这么写这题就没意思了)。但是,我一直在想,不增加空间消耗的情况下,如何进行排列,想了半天把自己绕进去了。开辟一个新数组,倒序放置就非常简单了。一定要利......
  • Fast Inverse Square Root
    FastInverseSquareRoot同时包含Approximationtheoryandmethodch11.https://www.youtube.com/watch?v=p8u_k2LIZyoFastInverseSquareRoot(快速倒数平方根)是一种算法,用于快速计算一个数的倒数平方根。该算法最早出现在QuakeIIIArena游戏引擎中,用于在计算机图形学中......
  • [USACO1.2]回文平方数 Palindromic Squares
    #[USACO1.2]回文平方数PalindromicSquares##题目描述回文数是指从左向右念和从右向左念都一样的数。如12321就是一个典型的回文数。给定一个用十进制表示的正整数B,输出所有[1,300]中,它的平方用B进制表示时是回文数的数。##输入格式共一行,一个单独的正整数B。##......
  • hdu 4758 Walk Through Squares(AC自动机+DP,4级)
    WalkThroughSquaresTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:65535/65535K(Java/Others)TotalSubmission(s):234    AcceptedSubmission(s):58ProblemDescription  Onthebeamingdayof60thanniversaryofNJUST,asamilitary......
  • Range Pair Distance Query
    洛谷6778给定一棵\(n\)个点的树,边带权(\(<2^{32}\)),\(q\)次查询\(\sum_{l\lei<j\ler}dis(i,j)\)。其中\(dis(i,j)\)代表点\(i\)到\(j\)的距离。......
  • HammingDistance
    汉明距离implementation'org.apache.commons:commons-text:1.10.0'Thehammingdistancebetweentwostringsofequallengthisthenumberofpositionsatwhichthecorrespondingsymbolsaredifferent.ForfurtherexplanationabouttheHammingDistan......