1 回答
![?](http://img1.sycdn.imooc.com/545847d40001cbef02200220-100-100.jpg)
TA贡献1852条经验 获得超1个赞
我不认为这是完美的,并且我肯定还有很多地方可以改进,但是这里有一个对您的示例都适用的解决方案(可能还有其他一些极端情况),我将其写为如何解决此问题的示例。首先,我们计算所有选项,然后检查哪些节点满足所有条件。一定要注意LowerCase游戏,这很重要!
public class Main {
public static void main(String[] args) {
// write your code here
Node exampleNode = new Node();
exampleNode.mySentence="academy concern manager".toLowerCase();
exampleNode.chars="ACM".toLowerCase().toCharArray();
exampleNode.nextNode();
System.out.println(getOutput(exampleNode));
exampleNode = new Node();
exampleNode.mySentence="Radio addition ranger".toLowerCase();
exampleNode.chars="RADAR".toLowerCase().toCharArray();
exampleNode.nextNode();
System.out.println(getOutput(exampleNode));
}
public static int getOutput(Node node){
int output=0;
if(node.chars.length==0&& node.mySentence.indexOf(" ")==-1){
Node nextNode=node;
String s="";
while (nextNode!=null){
String sub =nextNode.myChar+nextNode.mySentence;
s=replaceLast(s.toLowerCase(),"",sub)+s;
nextNode = nextNode.father;
}
boolean wordWithoutChar = false;
for (String word:s.split(" ")) {
boolean noCharInThisWord=true;
for (char c:word.toCharArray()) {
if(c<='Z'&&c>='A'){
noCharInThisWord = false;
}
}
if(noCharInThisWord){
wordWithoutChar=noCharInThisWord;
}
}
if(!wordWithoutChar){
System.out.println(s);
output++;
}
}
for (Node n:node.nodes) {
output +=getOutput(n);
}
return output;
}
public static String replaceLast(String find, String replace, String string) {
int lastIndex = string.lastIndexOf(find);
if (lastIndex == -1) {
return string;
}
String beginString = string.substring(0, lastIndex);
String endString = string.substring(lastIndex + find.length());
return beginString + replace + endString;
}
public static class Node{
List<Node> nodes = new ArrayList<>();
Node father =null;
char myChar=0;
char[] chars=null;
String mySentence="";
public void nextNode() {
int index=0;
if(chars.length>0){
while (index<mySentence.length()){
if(mySentence.toCharArray()[index]==chars[0]){
Node son = new Node();
son.chars = Arrays.copyOfRange(chars, 1, chars.length);
son.mySentence=mySentence.substring(index+1);
son.father=this;
son.myChar= (char) (chars[0]-32);
son.nextNode();
nodes.add(son);
}
index++;
}
}
return;
}
}
}
输出:
Academy Concern Manager
Academy conCern Manager
acAdemy Concern Manager
acAdemy conCern Manager
4
RADio Addition Ranger
RADio Addition rangeR
RAdio aDdition rAngeR
RAdio adDition rAngeR
Radio ADdition rAngeR
Radio AdDition rAngeR
6
添加回答
举报