添加最少字符使字符串整体都是回文字符串

//添加最少字符使字符串整体都是回文字符串
public class HuiString{
	
	//动态规划法求解
	//(1)获得动态规划表
	public static int[][]getDp(char[]str)
	{ 
		int[][]dp=new int[str.length][str.length];
		for(int j=1;j<str.length;j++)
		{
           dp[j-1][j]=str[j-1]==str[j]?0:1;
           for(int i=j-2;i>-1;i--)
           {
           	 if(str[i]==str[j])
           	 {
           	 	dp[i][j]=dp[i+1][j-1];
           	 }else{

           	 	dp[i][j]=Math.min(dp[i+1][j],dp[i][j-1]+1);
           	 }
           }
		}
         return dp;
	}

	//(2)根据动态规划表求出最短的回文串
	public static String GetHuiString(String str)
	{
		if(str==null||str.length()<2)
		{
			return str;
		}
		char[]chs=str.toCharArray(); //字符串转换为数组
		int[][]dp=getDp(chs); //获得动态规划数组
		char[]res=new char[chs.length+dp[0][chs.length-1]];  //最终字符串数组的长度
		int i=0;
		int j=chs.length-1;
		int resl=0;
		int resr=res.length-1;
		//对当前字符串的遍历
		while(i<=j)
		{ 
			if(chs[i]==chs[j])
			{
				res[resl++]=chs[i++];
				res[resr--]=chs[j--];
			}else if(dp[i][j-1]<dp[i+1][j])
			{
				res[resl++]=chs[j];
				res[resr--]=chs[j--];
			}else{

				res[resl++]=chs[i];
				res[resr--]=chs[i++];
			}

		}

        return String.valueOf(res);
	}
   //进阶问题解法
 	public static String getPalindrome2(String str, String strlps) {
		if (str == null || str.equals("")) {
			return "";
		}
		char[] chas = str.toCharArray();
		char[] lps = strlps.toCharArray();
		char[] res = new char[2 * chas.length - lps.length];
		int chasl = 0;
		int chasr = chas.length - 1;
		int lpsl = 0;
		int lpsr = lps.length - 1;
		int resl = 0;
		int resr = res.length - 1;
		int tmpl = 0;
		int tmpr = 0;
		while (lpsl <= lpsr) {
			tmpl = chasl;
			tmpr = chasr;
			while (chas[chasl] != lps[lpsl]) {
				chasl++;
			}
			while (chas[chasr] != lps[lpsr]) {
				chasr--;
			}
			set(res, resl, resr, chas, tmpl, chasl, chasr, tmpr);
			resl += chasl - tmpl + tmpr - chasr;
			resr -= chasl - tmpl + tmpr - chasr;
			res[resl++] = chas[chasl++];
			res[resr--] = chas[chasr--];
			lpsl++;
			lpsr--;
		}
		return String.valueOf(res);
	}

	public static void set(char[] res, int resl, int resr, char[] chas, int ls,
			int le, int rs, int re) {
		for (int i = ls; i < le; i++) {
			res[resl++] = chas[i];
			res[resr--] = chas[i];
		}
		for (int i = re; i > rs; i--) {
			res[resl++] = chas[i];
			res[resr--] = chas[i];
		}
	}
	public static void main(String[]args)
	{

		String  str1="ABA";
		String  str2="AB";
		System.out.println(GetHuiString(str1));
		System.out.println(GetHuiString(str2));
       
         //进阶问题的解法
		String str3="AB1CD2EFG3H43IJK2L1MN";
		String strlps = "1234321";
		System.out.println(getPalindrome2(str3, strlps));

	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要使一个字符串变成回文串,需要添加最少字符数取决于字符串本身。如果字符串本身已经是回文串,则不需要添加任何字符。否则,需要添加字符使其变成回文串。 可以使用动态规划来解决这个问题。具体来说,假设字符串为s,长度为n,定义dp[i][j]表示子串s[i...j]需要添加最少字符数,使得该子串变成回文串。则有以下状态转移方程: 当i = j时,dp[i][j] = 0,因为单个字符本身就是回文串。 当s[i] = s[j]时,dp[i][j] = dp[i+1][j-1],因为如果s[i]和s[j]相等,那么只需要让子串s[i+1...j-1]变成回文串即可。 否则,dp[i][j] = min(dp[i+1][j], dp[i][j-1]) + 1,因为如果s[i]和s[j]不相等,那么必须要在s[i]和s[j]中间添加一个字符,使得子串s[i...j]变成回文串,那么这个字符可以是s[i],也可以是s[j],所以需要取添加字符后变成回文串所需的最小次数。 最终,dp[0][n-1]就是整个字符串s需要添加最少字符数,使得它变成回文串。 ### 回答2: 要使字符串str变回文串,需要在字符串的前后添加若干个字符添加字符的数量最少是str的长度减去str的回文子串的长度。 首先,我们需要找出str的最长回文子串的长度。可以通过遍历字符串的所有子串,判断是否为回文串,从而得到最长回文子串的长度。 接下来,将str的长度减去最长回文子串的长度,得到需要添加字符的数量。因为添加字符需要同时出现在str的前后,所以数量是原字符串长度和最长回文子串长度的差值。 举个例子,假设str为"abcbade",该字符串的最长回文子串为"abcba",长度为5。字符串str的长度为7,所以需要添加字符数量为7-5=2。 所以需要添加至少2个字符才能使字符串str变回文串。 ### 回答3: 已知字符串str,如果它本身就是一个回文串,那么不需要添加任何字符;如果str不是一个回文串,我们需要添加至少的字符使它变成一个回文串。 为了使str变成一个回文串,我们可以在字符串的某个位置添加字符。考虑一个最简单的情况,我们将添加字符在str的最前面,即在str的首字符添加一个与str首字符相同的字符。这样,str的首字符和最前添加字符都是相同的,这种情况下,我们只需要添加一个字符。 但是,如果str的首字符和尾字符不相同,我们必须至少添加两个字符。我们可以在str的首字符添加一个与str首字符相同的字符,再在str的尾字符添加一个与str尾字符相同的字符。这样,在添加的两个字符的帮助下,str首字符和最前添加字符相同,str尾字符和最后添加字符相同,形成了一个回文串。 所以,总结起来,我们需要至少添加字符数就等于str的首字符和尾字符不相同的情况下需要添加字符数。即如果str的首字符和尾字符相同,我们只需要添加一个字符;如果不相同,我们需要添加两个字符。 综上所述,已知字符串str,至少需要添加字符数为1或2,具体取决于str的首字符和尾字符是否相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值