public static String cutTree(String treeStr, String paramStr){ String tempStr = null; String resultStr = null; //记录括弧位置,treeMap自动按照key排序 TreeMap<Integer, Integer> bracketPositionMap = new TreeMap<Integer, Integer>(); //获取扁平串中括弧的位置 for (int i = 0; i < treeStr.length(); i++) { if (treeStr.charAt(i) == '[') { bracketPositionMap.put(i, 0); }else if (treeStr.charAt(i) == ']') { bracketPositionMap.put(i, 1); } } Iterator<Integer> it = bracketPositionMap.keySet().iterator(); int position = treeStr.indexOf(paramStr)-1; int startPosition = position; int endPosition = 0; int leftNum = 0;//记录左括号的个数 int rightNum = 0;//记录右括号的个数 if(position == 0){ resultStr = ""; }else if(position == -2){ resultStr = treeStr; }else{ while (it.hasNext()){ endPosition = Integer.valueOf(it.next().toString()); if(endPosition <= position){ continue; } if (bracketPositionMap.get(startPosition) == 0 ) { leftNum++; }else if (bracketPositionMap.get(startPosition) == 1) { //如果是以右括号开始的,必然为空值,不截取 rightNum++; } if(leftNum == rightNum){ break; } startPosition = endPosition; } tempStr = treeStr.substring(position, endPosition); resultStr = treeStr.replace(tempStr, ""); if(resultStr.indexOf(paramStr + "[") != -1 || resultStr.indexOf(paramStr + "]") != -1){ resultStr = cutTree(resultStr,paramStr); } } return resultStr; }
package algorithm;/** * * Author`s mentor: <b>Dr. Fan Min</b> <br> * Author: <b>Yanxue Wu </b> <br> * Email: [email protected] <br> * Copyright: The source code and all documents are open and free. PLEASE keep * this header while revising the program. <br> * Organization: <a href=http://scs.swpu.edu.cn/smale/>Lab of Machine * Learning</a>, Southwest Petroleum University, Chengdu 610500, China.<br> */ public class Test { /** **************************** * We assume '[]' as a set. The method aims to delete the substring which * likes '[]' set and its subset and contains the given pivot char. * * @param paraStr * the given string. * @param paraPivot * the given pivot. * @return the string after truncated. **************************** */ public static String truncate(String paraStr, String paraPivot) {
int tempIndex = paraStr.indexOf(paraPivot);
if(tempIndex == -1) { return paraStr; }// Of if
int tempCounter = 1;
// Record the location of '['. int tempLeftBracketLoc = 0;
// Record the location of ']'. int tempRightBracketLoc = 0;
for(int i = tempIndex - 1; i >= 0; i--) { if(paraStr.charAt(i) == '[') { tempLeftBracketLoc = i; break; }// Of if
}// Of for i
for(int i = tempIndex; i < paraStr.length(); i++) { if(paraStr.charAt(i) == '[') { tempCounter++; }// Of if
if(paraStr.charAt(i) == ']') { tempCounter--; }// Of if
if(tempCounter == 0) { tempRightBracketLoc = i; break; }// Of if
return tempString; }// Of truncate /** **************************** * Testing method. **************************** */ public static void main(String[] args) { String tempStr = "[a[b[c]][d[e]]]"; tempStr = truncate(tempStr, "b"); System.out.println(tempStr); }// Of main }// Of class Test 你应该能看懂。
想了一下,有两个思路:思路一:把'[','a',']'这些单元split开,往一下Stack里塞,当碰到和参数x相同的时,进入另一个模式,再输入的就忽略掉(或者用计数'['的方式),直到碰到与x对应的右边的']',需要去掉左边的[x, 剩下的即是想要的串。思路二:构建一棵树,按参数删除子树,再将剩下的树反向成字符串。
String resultStr = null;
//记录括弧位置,treeMap自动按照key排序
TreeMap<Integer, Integer> bracketPositionMap = new TreeMap<Integer, Integer>();
//获取扁平串中括弧的位置
for (int i = 0; i < treeStr.length(); i++) {
if (treeStr.charAt(i) == '[') {
bracketPositionMap.put(i, 0);
}else if (treeStr.charAt(i) == ']') {
bracketPositionMap.put(i, 1);
}
}
Iterator<Integer> it = bracketPositionMap.keySet().iterator();
int position = treeStr.indexOf(paramStr)-1;
int startPosition = position;
int endPosition = 0;
int leftNum = 0;//记录左括号的个数
int rightNum = 0;//记录右括号的个数
if(position == 0){
resultStr = "";
}else if(position == -2){
resultStr = treeStr;
}else{
while (it.hasNext()){
endPosition = Integer.valueOf(it.next().toString());
if(endPosition <= position){
continue;
}
if (bracketPositionMap.get(startPosition) == 0 ) {
leftNum++;
}else if (bracketPositionMap.get(startPosition) == 1) {
//如果是以右括号开始的,必然为空值,不截取
rightNum++;
}
if(leftNum == rightNum){
break;
}
startPosition = endPosition;
}
tempStr = treeStr.substring(position, endPosition);
resultStr = treeStr.replace(tempStr, "");
if(resultStr.indexOf(paramStr + "[") != -1 || resultStr.indexOf(paramStr + "]") != -1){
resultStr = cutTree(resultStr,paramStr);
}
}
return resultStr;
}
*
* Author`s mentor: <b>Dr. Fan Min</b> <br>
* Author: <b>Yanxue Wu </b> <br>
* Email: [email protected] <br>
* Copyright: The source code and all documents are open and free. PLEASE keep
* this header while revising the program. <br>
* Organization: <a href=http://scs.swpu.edu.cn/smale/>Lab of Machine
* Learning</a>, Southwest Petroleum University, Chengdu 610500, China.<br>
*/
public class Test { /**
****************************
* We assume '[]' as a set. The method aims to delete the substring which
* likes '[]' set and its subset and contains the given pivot char.
*
* @param paraStr
* the given string.
* @param paraPivot
* the given pivot.
* @return the string after truncated.
****************************
*/
public static String truncate(String paraStr, String paraPivot) {
int tempIndex = paraStr.indexOf(paraPivot);
if(tempIndex == -1) {
return paraStr;
}// Of if
int tempCounter = 1;
// Record the location of '['.
int tempLeftBracketLoc = 0;
// Record the location of ']'.
int tempRightBracketLoc = 0;
for(int i = tempIndex - 1; i >= 0; i--) {
if(paraStr.charAt(i) == '[') {
tempLeftBracketLoc = i;
break;
}// Of if
}// Of for i
for(int i = tempIndex; i < paraStr.length(); i++) {
if(paraStr.charAt(i) == '[') {
tempCounter++;
}// Of if
if(paraStr.charAt(i) == ']') {
tempCounter--;
}// Of if
if(tempCounter == 0) {
tempRightBracketLoc = i;
break;
}// Of if
}// Of for i
String tempString = paraStr.substring(0, tempLeftBracketLoc);
tempString += paraStr.substring(tempRightBracketLoc, paraStr.length());
return tempString;
}// Of truncate /**
****************************
* Testing method.
****************************
*/
public static void main(String[] args) {
String tempStr = "[a[b[c]][d[e]]]";
tempStr = truncate(tempStr, "b");
System.out.println(tempStr);
}// Of main
}// Of class Test
你应该能看懂。