求字符串中不含重复字符的最长子串的长度

news/2024/7/6 3:03:40

题目:求字符串最长不含重复字符的子串长度,如abcbec,就返回3.

分析:

利用动态规划(DP)原理,设字符串S的长度为n,考虑i...n-1这个后缀中符合条件的子串:首先需要记录两组数据,第一组数据是从i向右找到的最长不含重复字符的子串长度prefixlen[i],第二组数据是在i...n-1后缀中符合条件的子串之起始和结束位置,分别用 maxlenstart[i]和maxlenend[i]表示,注意二组数据满足:maxlenstart[i]-maxlenend[i]+1>=prefixlen[i],即从S[i]开始最左边的最长不含重复字符的子串长度与区间i...n-1中符合条件的子串长度可能相等;如果S[i]给prefixlen[i+1]增加一个字符长度,说明S[i]与右边长度为 prefixlen[i+1]个字符都不相同,这时需要分两种情况考虑,1)如果maxlenstart[i+1]==i+1即S[i+1...n-1] 中得到的最优子串的左边一个字符与S[i]也相邻,说明最大子串长度也应该增加1, 即设置maxlenstart[i]=i;注意此时如果prefixlen[i]总长度未在prefixlen[i+1]的基础上加1,说明第二组数据的长度不会比第一组数据长度大了,这是因为根据DP的计算过程,只要第二组数据与S[i]相邻,那么两组数据长度保证了前一步中是相等的;2)如果第二组数据不与S[i]相邻,则需要比较二组数据的长度了。

具体实现时需要注意边界条件,设置prefixlen[n] = 0; maxlenstart[n] = n; maxlenend[n] = n-1;。

代码:

/**
 * 
 * @author ljs
 * 2011-07-09
 *
 */
public class LongestSubseqWithUniqueChars {

	public static int solve(String str){
		int n = str.length();
		int[] prefixlen=new int[n+1];
		prefixlen[n] = 0;
		int[] maxlenstart = new int[n+1];
		maxlenstart[n] = n;
		int[] maxlenend = new int[n+1];
		maxlenend[n] = n-1;
		
		for(int i=n-1;i>=0;i--){
			char c = str.charAt(i);
			//caculate prefixlen[i] by prefixlen[i+1]
			int j=0,k;
			for(j=0,k=i+1;j<prefixlen[i+1];j++,k++){
				if(c == str.charAt(k)){					
					break;
				}
			}
			prefixlen[i] = j+1;
			
			//caculate maxlenstart[i] and maxlenend[i]
			maxlenstart[i] = maxlenstart[i+1];
			maxlenend[i] = maxlenend[i+1];
			if(maxlenstart[i+1] == i+1){
				if(prefixlen[i] == prefixlen[i+1] + 1){
					maxlenstart[i] = i;
				}
			}else{
				//update the max len for i...n-1
				if(maxlenend[i] - maxlenstart[i] + 1 < prefixlen[i]){
					maxlenstart[i] = i;
					maxlenend[i] = i + prefixlen[i] - 1;
				}
			}			
		}
		return maxlenend[0] - maxlenstart[0] + 1;
	}
	public static void main(String[] args) {
		String str = "abcbec";
		int maxlen = LongestSubseqWithUniqueChars.solve(str);
		System.out.format("max len of substring with unique chars for string \"%s\" = %d.%n",
				str,maxlen);
		
		str = "adabcbec";
		maxlen = LongestSubseqWithUniqueChars.solve(str);
		System.out.format("max len of substring with unique chars for string \"%s\" = %d.%n",
				str,maxlen);
		
		str = "abadadabbc";
		maxlen = LongestSubseqWithUniqueChars.solve(str);
		System.out.format("max len of substring with unique chars for string \"%s\" = %d.%n",
				str,maxlen);
		
		str = "ffdeefghff";
		maxlen = LongestSubseqWithUniqueChars.solve(str);
		System.out.format("max len of substring with unique chars for string \"%s\" = %d.%n",
				str,maxlen);
		
		str = "abcbecghijkl";
		maxlen = LongestSubseqWithUniqueChars.solve(str);
		System.out.format("max len of substring with unique chars for string \"%s\" = %d.%n",
				str,maxlen);
		
	}

}

 

测试输出:

max len of substring with unique chars for string "abcbec" = 3.
max len of substring with unique chars for string "adabcbec" = 4.
max len of substring with unique chars for string "abadadabbc" = 3.
max len of substring with unique chars for string "ffdeefghff" = 4.
max len of substring with unique chars for string "abcbecghijkl" = 9.

转载于:https://www.cnblogs.com/xulb597/archive/2012/05/23/2514483.html


http://www.niftyadmin.cn/n/673095.html

相关文章

理财周报特别策划:关注601开头的大股票

http://www.sina.com.cn 2007年09月09日 20:48 理财周报大象起舞&#xff0c;独步武林。以“601”开头的25只股票&#xff0c;理财周报称之为“601大股票”。 “601”股票代码最早于2006年5月底正式使用。探询“601”的存在意义已无必要&#xff0c;关键是“601”能带来什么。理…

(原)linux下编译microhttpd库(一个c/c++的http服务端库)

下载库&#xff1a; http://www.gnu.org/software/libmicrohttpd/这里有简单的该库介绍以及使用方法&#xff08;简略&#xff09;。下载&#xff1a;$ svn checkout https://gnunet.org/svn/libmicrohttpd/使用手册&#xff1a;http://www.gnu.org/software/libmicrohttpd/mic…

9月10日金股

http://www.jrj.com  2007年09月07日 17:44 金融界网站 【字体&#xff1a;大 中 小】 【页面调色版 】 走 势摘 要 厦华电子&#xff08;行情,资讯&#xff09;(600870)&#xff1a;随着电子行业利润持续、高速增长&#xff0c;特别是数字电视的不断推广&#xff0c;公司…

创业板前夜 比八年前更猛烈的财富冲浪

2007-9-10 8:27:00 代码:作者:朱凯栋 卢山林 李晔斌 来源: 出处: 理财周报加入收藏复制链接给好友跳到低部深交所内部人士向理财周报记者透露&#xff0c;目前创业板指数系统已进入研发阶段&#xff0c;创业板上市推广部已经往西北、华北、东北、华南等区域派驻了工作人员&…

年入10万如何理财 筑巢期组建幸福小家理财计划

[作者]纪红霞 [来源]每日商报 [选稿]summer 2007-09-10 09:53:32 理财顾问 招商银行杭州分行财富管理理财师 阮肖林 理财格言 专业、专注、诚信、创新 理财案例&#xff1a; 刘小姐咨询&#xff1a;我和准老公今年均为26岁&#xff0c;参加工作四年&#xff0c;两人…

滞涨蓝筹一鸣惊人

http://www.jrj.com  2007年09月10日 16:52 金融界网站 【字体&#xff1a;大 中 小】 【页面调色版 】 受外部利淡因素的影响&#xff0c;周一沪深股指跳空低开&#xff0c;上证综指最低运行到5169.91点后振荡回升&#xff0c;并伴随着成交量的支持一路震荡上行&#xff…

AndroidWidget实践 --- EverydayTips开发(4)

接下来就是刷新了.刷新操作的话目测有几种方式(目测 --!) 1.在widget创建线程刷新 2.使用timer刷新(其实也是线程吧?) 3.widget连接Service 在Service创建线程刷新 4.widget连接Service 在Service中使用AlarmManager刷新 Thread比较简单,修改widget代码如下 package com.su.ti…

广电信息:超跌低价本地股 业绩增长190%

http://www.jrj.com  2007年09月10日 17:10 金融界网站 【字体&#xff1a;大 中 小】 【页面调色版 】 周一大盘的表现正如我们上周末所谈到的一样&#xff0c;不排除周一大盘以阳包阴的态势展开。从周一股指止跌后展开的稳步盘升的走势来看&#xff0c;表明当前行情仍处…