http://www.1point3acres.com/bbs/thread-148741-1-1.html
函数签名为 int countChunk(String input), 给定一个字符串,找出最多有多少个chunked palindrome,
正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起,(vo) (l) (vo),那么它是个palindrome。求实现返回最大的chunk 数量。
是的,是要返回chunk的最大数目~所以vovo返回2,voabcvo返回3
voabcvo的话就是算3个了,分法就是按照你的说的一样:(vo)(abc)(vo)
因为最多只能分成这三块,让它“以块为单位”是回文:相当于A=vo,B=abc,原字符串=ABA。
(vo)(l)(vo)(l)的话就不能这么分了,因为他不是回文,正确的划分为(volvol),函数返回1.
(vo)(l)(vo)(l)(vo) 就是这么分的,函数返回5。
http://www.cnblogs.com/EdwardLiu/p/5136947.html
public static int chunkNum(String s){
if(s == null || s.length() == 0){
return 0;
}
int length = s.length();
int[][] DP = new int[length][length];
for(int i=length-1;i>=0;i--){
for(int j=i;j<length;j++){
if(i == j){
DP[i][j] = 1;
}
else{
int sum = 0;
int mid = i + (j-i) / 2;
for(int count=i;count<=mid;count++){
String pre = s.substring(i,count+1);
String post = s.substring(j-count+i,j+1);
if(pre.equals(post)){
sum += DP[count+1][j-count+i-1];
}
}
DP[i][j] = sum;
}
}
}
return DP[0][length-1];
}
int countMaxChunks(string str) {
int count = 0;
int i = 0, j = str.size()-1; // scan from the two sides
int prev_i = i, prev_j = j;
while(i<j) {
string chunk1 = str.substr(prev_i, i+1);
string chunk2 = str.substr(j, prev_j+1);
if (chunk1==chunk2) {
count++;
prev_i = i+1;
prev_j = j-1;
}
i++;
j--;
}
count *= 2;
// if odd string
if (i==j)
count++;
// even string and still has chunk left
else if (prev_i<prev_j)
count++;
return count;
}
补充内容 (2015-11-24 01:37):
思想比较简单,但是从string的两边开始,用i和j记录当前scan到的位置,用prev_i和prev_j记录上一次找到chunk的i和j的位置的下一个字符。最后扫到中间判断一下有无多余的chunk。
函数签名为 int countChunk(String input), 给定一个字符串,找出最多有多少个chunked palindrome,
正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起,(vo) (l) (vo),那么它是个palindrome。求实现返回最大的chunk 数量。
是的,是要返回chunk的最大数目~所以vovo返回2,voabcvo返回3
voabcvo的话就是算3个了,分法就是按照你的说的一样:(vo)(abc)(vo)
因为最多只能分成这三块,让它“以块为单位”是回文:相当于A=vo,B=abc,原字符串=ABA。
(vo)(l)(vo)(l)的话就不能这么分了,因为他不是回文,正确的划分为(volvol),函数返回1.
(vo)(l)(vo)(l)(vo) 就是这么分的,函数返回5。
比如aaaaaa可以是(aaa)(aaa), 但是最大chunk数量应该是(a)(a)(a)(a)(a)(a)为6
这就是一个greedy的问题,从string的两边开始,用i和j记录当前scan到的位置,用prev_i和prev_j记录上一次找到chunk的i和j的位置的下一个字符。最后扫到中间判断一下有无多余的chunk。
时间复杂度为O(N^2), 内层string.equals 有O(N)复杂度
4 public int countChunk(String str) { 5 if (str==null || str.length()==0) return 0; 6 int sum = 0; 7 int l=0, r=str.length()-1; 8 int preL = l, preR = r; 9 while (l < r) { 10 String left = str.substring(preL, l+1); 11 String right = str.substring(r, preR+1); 12 if (left.equals(right)) { 13 preL = l+1; 14 preR = r-1; 15 sum += 2; 16 } 17 l++; 18 r--; 19 } 20 if (preL <= preR) sum+=1; 21 return sum; 22 }http://www.geeksforgeeks.org/longest-possible-chunked-palindrome/
The entire idea is to create chunks from left and right and recursively.
As you can see, we can match substring from left side chunck and match it with the exact right side chunk. Once we get a match, we recursively count the length of the longest possible chunked palindrome in the remaining string. We end the recursion once no string is left or when no more valid chunked parts can be found.
private
static
int
LPCRec(String curr_str,
int
count,
int
len, String str)
{
// if there is noting at all!!
if
(curr_str ==
null
|| curr_str.isEmpty())
return
(
0
);
// if a single letter is left out
if
(curr_str.length() <=
1
)
{
if
(count !=
0
&& str.length() - len <=
1
)
return
(count +
1
);
else
return
(
1
);
}
// for each length of substring chunk in string
int
n = curr_str.length();
for
(
int
i =
0
; i < n/
2
; i++)
{
// if left side chunk and right side chunk
// are same
if
(curr_str.substring(
0
, i +
1
).
equals(curr_str.substring(n-
1
-i, n)))
{
// Call LCP for the part between the
// chunks and add 2 to the result.
// Length of string evaluated till
// now is increased by (i+1)*2
return
LPCRec(curr_str.substring(i+
1
, n-
1
-i),
count +
2
,
len + (i+
1
)*
2
, str);
}
}
return
count +
1
;
}
// Wrapper over LPCRec()
public
static
int
LPC(String str)
{
return
LPCRec(str,
0
,
0
, str);
}
public static int chunkNum(String s){
if(s == null || s.length() == 0){
return 0;
}
int length = s.length();
int[][] DP = new int[length][length];
for(int i=length-1;i>=0;i--){
for(int j=i;j<length;j++){
if(i == j){
DP[i][j] = 1;
}
else{
int sum = 0;
int mid = i + (j-i) / 2;
for(int count=i;count<=mid;count++){
String pre = s.substring(i,count+1);
String post = s.substring(j-count+i,j+1);
if(pre.equals(post)){
sum += DP[count+1][j-count+i-1];
}
}
DP[i][j] = sum;
}
}
}
return DP[0][length-1];
}
int countMaxChunks(string str) {
int count = 0;
int i = 0, j = str.size()-1; // scan from the two sides
int prev_i = i, prev_j = j;
while(i<j) {
string chunk1 = str.substr(prev_i, i+1);
string chunk2 = str.substr(j, prev_j+1);
if (chunk1==chunk2) {
count++;
prev_i = i+1;
prev_j = j-1;
}
i++;
j--;
}
count *= 2;
// if odd string
if (i==j)
count++;
// even string and still has chunk left
else if (prev_i<prev_j)
count++;
return count;
}
补充内容 (2015-11-24 01:37):
思想比较简单,但是从string的两边开始,用i和j记录当前scan到的位置,用prev_i和prev_j记录上一次找到chunk的i和j的位置的下一个字符。最后扫到中间判断一下有无多余的chunk。