首页 > 8.15 携程第二道编程java实现
头像
docli
编辑于 2020-08-15 20:28
+ 关注

8.15 携程第二道编程java实现

package allcoding;
import java.util.*;
import java.util.stream.Collectors;

class WorkflowNode {
String nodeId;
int timeoutMillis;
List<WorkflowNode> nextNodes;
boolean initialised;

public static WorkflowNode load(String value) {//传入字符串
// Create head node;
Map<String, WorkflowNode> map = new HashMap<>();// 存储id和节点
WorkflowNode head = new WorkflowNode("HEAD", 0, null);//id,time,nexttime
map.put(head.nodeId, head);//

for (String nodeValue : value.split("\\|")) {//分割节点
String[] properties = nodeValue.split("\\`");//分割毫秒
WorkflowNode node = map.get(properties[0]);//获取head

node.timeoutMillis = Integer.parseInt(properties[1]);//获取时间
node.initialised = true;
// Check next nodes
if (properties[2].equals("END")) {//为end的情况
continue;
}
node.nextNodes = Arrays.stream(properties[2].split(","))
.map(p -> new WorkflowNode(p, 0, null))
.collect(Collectors.toList());
node.nextNodes.forEach(p -> map.put(p.nodeId, p));

map.put(node.nodeId, node);
}

return head;
}

public WorkflowNode(String nodeId, int timeoutMillis, List<WorkflowNode> nextNodes) {
this.nodeId = nodeId;
this.timeoutMillis = timeoutMillis;
this.nextNodes = nextNodes;
}
}

public class Demo
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while (cin.hasNext())
{
WorkflowNode node = WorkflowNode.load(cin.next());
System.out.println(text(node));
}

}
public static int text(WorkflowNode root){
if(root==null) return 0;
List<WorkflowNode> nextNodes = root.nextNodes;
if(nextNodes!=null) {
int [] aa = new int[nextNodes.size()];
WorkflowNode temp;
for(int i= 0;i<nextNodes.size();i++){
temp= (WorkflowNode)nextNodes.get(i);
aa[i] = text(temp)+root.timeoutMillis;
}
Arrays.sort(aa);
return aa[aa.length-1];
}else {
return root.timeoutMillis;
}
}
}
//HEAD`0`A,B,C|A`20`END|B`100`END|C`50`D,E|D`80`F|E`150`END|F`30`END

全部评论

(2) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐