求字符串中不含重复字符的最长子串的长度
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;。
代码:
测试输出:
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);
分析:
利用动态规划(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);
相关文章推荐
- 求字符串中不含重复字符的最长子串的长度
- 求字符串最长不含重复字符的子串长度
- 百度面试题 求字符串中不含重复字符的最长子串长度
- 字符串最长不含重复字符的子串长度
- 给定一个字符串,返回字符串中没有重复字符的最长子串的长度
- 给定一个字符串,找到最长的子串的长度没有重复字符
- 求给定的某一个字符串中的最长的没有重复字符的子串的长度
- 3. Longest Substring Without Repeating Characters(计算不含重复字符的最长子串的长度)
- 定义一个栈的数据结构,实现min函数,要求push,pop,min时间复杂度是0(1);找出字符串中的最长子串,要求子串不含重复字符,时间复杂度是O(n);
- 求字符串中不含重复字符的最长子串
- (字符串哈希表)找到字符串中不重复出现字符的最长子串长度
- 求一字符串最长不重复字符子串的长度【Java 版】
- 求字符串的不重复字符的最长子串长度的问题
- 在英文字符串中找第一个最长不含重复字符的子串高效实现(修改版)
- 求字符串的不重复字符的最长子串长度的问题
- [leetcode]获取字符串中字符不重复的最长子串的长度
- 定义一个栈的数据结构,实现min函数,要求push,pop,min时间复杂度是0(1);找出字符串中的最长子串,要求子串不含重复字符,时间复杂度是O(n);
- C++找出字符串中最长的不含重复字符的子串
- leetcode第三题:求给定字符串中最长无重复子串的长度
- 字符串空格替换、合法括号序列判断、最长无重复子串长度