原帖:http://topic.csdn.net/u/20100930/02/5064ef5a-b2f4-44b9-8a16-ce2bf83323c0.html?84726
题目:求字符串最长不含重复字符的子串长度,如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.