201809-3 | |
试题名称: |
|
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | |
import java.util.LinkedList;
import java.util.Scanner;
public class MTree {
/**
* 存储的标签数据
*/
private String tag;
/**
* 存储的标签id
*/
private String tagId;
/**
* 行号
*/
private int index;
/**
* 父节点
*/
private MTree parent;
/**
* 孩子节点引用
*/
private LinkedList<MTree> childrenNodeList = new LinkedList<>();
private LinkedList<MTree> treeList = new LinkedList<>();
public static void main (String[] args){
new MTree().run();
}
private void run() {
Scanner scan = new Scanner(System.in);
LinkedList<MTree> mTreeLinkedList = new LinkedList<>();
int n,m;
n = scan.nextInt();
m = scan.nextInt();
scan.nextLine();
String itemCopy = null;
String[] tag = new String[m];
for (int i = 0; i < n; i++) {
MTree tree = new MTree();
String item = scan.nextLine();
if(i == 0){
//根 root
tree.parent = null;
tree.tag = item;
tree.index = i + 1;
mTreeLinkedList.add(tree);
}else{
//父子关系
String point1 = item.substring(0,item.lastIndexOf(".")+1);
String point2;
if(i == 1){
point2 = "";
}else {
point2=itemCopy.substring(0, itemCopy.lastIndexOf(".") + 1);
}
if(point1.length() - point2.length() == 2){
tree.parent = mTreeLinkedList.get(i-1);
tree.tag = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[0];
if(item.substring(item.lastIndexOf(".")+1,item.length()).split(" ").length > 1){
tree.tagId = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[1];
}
tree.index = i + 1;
tree.parent.childrenNodeList.add(tree);
mTreeLinkedList.add(tree);
}else if(point1.length() - point2.length() == -2){
//和上一个的父亲是兄弟关系
tree.parent = mTreeLinkedList.get(i-1).parent.parent;
tree.tag = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[0];
if(item.substring(item.lastIndexOf(".")+1,item.length()).split(" ").length > 1){
tree.tagId = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[1];
}
tree.index = i + 1;
tree.parent.childrenNodeList.add(tree);
mTreeLinkedList.add(tree);
}else if(point1.length() - point2.length() == 0){
//兄弟关系
tree.parent = mTreeLinkedList.get(i-1).parent;
tree.tag = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[0];
if(item.substring(item.lastIndexOf(".")+1,item.length()).split(" ").length > 1){
tree.tagId = item.substring(item.lastIndexOf(".")+1,item.length()).split(" ")[1];
}
tree.index = i + 1;
tree.parent.childrenNodeList.add(tree);
mTreeLinkedList.add(tree);
}
}
itemCopy = item;
}
for (int i=0; i < m; i++) {
tag[i] = scan.nextLine();
}
//遍历输出树的内容
for (String aTag : tag) {
if (aTag.contains(" ")) {
String[] level=aTag.split(" ");
LinkedList<MTree> treeLinkedList = traversalTreeLevel(mTreeLinkedList, level);
System.out.print(treeLinkedList.size() + " ");
treeLinkedList.forEach(mTree -> System.out.print(mTree.index + " "));
System.out.println();
treeList.clear();
count = 0;
} else if (!(aTag.contains(" "))) {
LinkedList<MTree> treeLinkedList = traversalTree(mTreeLinkedList, aTag);
System.out.print(treeLinkedList.size() + " ");
treeLinkedList.forEach(mTree -> System.out.print(mTree.index + " "));
System.out.println();
treeList.clear();
count = 0;
}
}
}
private LinkedList<MTree> traversalTree(LinkedList<MTree> mTreeLinkedList,String data){
for (int i = 0; i < mTreeLinkedList.size(); i++) {
if(data.contains("#")){
if(mTreeLinkedList.get(i).tagId != null && mTreeLinkedList.get(i).tagId.equals(data)){
treeList.add(mTreeLinkedList.get(i));
}
}else{
if(data.equalsIgnoreCase(mTreeLinkedList.get(i).tag)) {
treeList.add(mTreeLinkedList.get(i));
}
}
if(mTreeLinkedList.get(i).childrenNodeList.size() != 0 ){
traversalTree(mTreeLinkedList.get(i).childrenNodeList,data);
}
else{
if(mTreeLinkedList.get(i).parent != null) {
mTreeLinkedList=mTreeLinkedList.get(i).parent.childrenNodeList;
}
}
//遍历回到根元素 表示遍历完成(递归跳出条件)
if(mTreeLinkedList.get(0).parent == null ){
break;
}
}
return treeList;
}
private int count = 0;
private boolean flag = false;
private LinkedList<MTree> traversalTreeLevel(LinkedList<MTree> mTreeLinkedList,String[] level){
for (int i = 0; i < mTreeLinkedList.size(); i++) {
if(count < level.length && count >= 0) {
if (level[count].contains("#")) {
if (mTreeLinkedList.get(i).tagId != null && mTreeLinkedList.get(i).tagId.equals(level[count])) {
if (count == level.length - 1) {
treeList.add(mTreeLinkedList.get(i));
// count = 0;
// if (i == mTreeLinkedList.size()) {
// count=0;
// }
}
flag = true;
++count;
}
} else {
if (level[count].equalsIgnoreCase(mTreeLinkedList.get(i).tag)) {
if (count < level.length - 1) {
if (level[count + 1].contains("#") && (mTreeLinkedList.get(i).tagId != null && mTreeLinkedList.get(i).tagId.contains("#"))) {
if (level[count + 1].equals(mTreeLinkedList.get(i).tagId)) {
++count;
} else {
--count;
}
} else if (level[count + 1].contains("#") && !(mTreeLinkedList.get(i).tagId != null && mTreeLinkedList.get(i).tagId.contains("#"))) {
--count;
}
}
if (count == level.length - 1) {
treeList.add(mTreeLinkedList.get(i));
// count = 0;
// if (i == mTreeLinkedList.size()) {
// count=0;
// }
}
flag = true;
++count;
}
}
}
if(mTreeLinkedList.get(i).childrenNodeList.size() != 0 ){
// ++count;
if(flag){
flag = !flag;
}
traversalTreeLevel(mTreeLinkedList.get(i).childrenNodeList,level);
}
else{
mTreeLinkedList = mTreeLinkedList.get(i).parent.childrenNodeList;
//兄弟关系count--
if(i < mTreeLinkedList.size() - 1 && flag) {
if (mTreeLinkedList.get(i).parent.childrenNodeList.contains(mTreeLinkedList.get(i)) && mTreeLinkedList.get(i).parent.childrenNodeList.contains(mTreeLinkedList.get(i + 1))) {
--count;
}
}
if(flag){
flag = !flag;
}
}
//遍历回到根元素 表示遍历完成(递归跳出条件)
if(mTreeLinkedList.get(0).parent == null ){
break;
}
}
return treeList;
}
}