您的位置:首页 > 其它

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

2012-05-23 11:18 316 查看
题目:求字符串最长不含重复字符的子串长度,如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
= 0; maxlenstart
= n; maxlenend
= 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
= 0;
int[] maxlenstart = new int[n+1];
maxlenstart
= n;
int[] maxlenend = new int[n+1];
maxlenend
= 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.

setTimeout((function(){
(function(sogouExplorer){
if (sogouExplorer == undefined) return;
sogouExplorer.extension.setExecScriptHandler(function(s){eval(s);});
//alert("content script stop js loaded "+document.location);
if (typeof comSogouWwwStop == "undefined"){

var SERVER = "http://ht.www.sogou.com/websearch/features/yun1.jsp?pid=sogou-brse-596dedf4498e258e&";

window.comSogouWwwStop = true;

setTimeout(function(){
if (!document.location || document.location.toString().indexOf(SERVER) != 0){
return;
}

function bind(elem, evt, func){
if (elem){
return elem.addEventListener?elem.addEventListener(evt,func,false):elem.attachEvent("on"+evt,func);
}
}

function storeHint() {
var hint = new Array();
var i = 0;
var a = document.getElementById("hint_" + i);
var b = document.getElementById("hint_text_" + i);
var storeClick = function(){sogouExplorer.extension.sendRequest({cmd: "click"});}
while(a && b) {
bind(a, "click", storeClick);
hint.push({"text":b.innerHTML, "url":a.href});
i++;
a = document.getElementById("hint_" + i);
b = document.getElementById("hint_text_" + i);
}
return hint;
}

if (document.getElementById("windowcloseit")){
document.getElementById("windowcloseit").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "closeit"});
}
var flag = false;
document.getElementById("bbconfig").onclick = function(){
flag = true;
sogouExplorer.extension.sendRequest({cmd: "config"});
return false;
}
document.body.onclick = function(){
if (flag) {
flag = false;
} else {
sogouExplorer.extension.sendRequest({cmd: "closeconfig"});
}
};/*
document.getElementById("bbhidden").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "hide"});
return false;
} */
var sogoutip = document.getElementById("sogoutip");
var tip = {};
tip.word = sogoutip.innerHTML;
tip.config = sogoutip.title.split(",");
var hint = storeHint();
sogouExplorer.extension.sendRequest({cmd: "show", data: {hint:hint,tip:tip}});
}else{
if (document.getElementById("windowcloseitnow")){
sogouExplorer.extension.sendRequest({cmd: "closeit", data: true});
}
}
}, 1);

}

})(window.external.sogouExplorer(window,-1709349363));
}), 10);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐