试题编号: 201809-3
试题名称: 元素选择器
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190316091756188.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDU2ODY3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190316091832591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDU2ODY3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019031609193178.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDU2ODY3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190316091902995.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMDU2ODY3,size_16,color_FFFFFF,t_70)
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
in.nextLine();
ArrayList<Label> parents=new ArrayList<Label>();
ArrayList<Label> list=new ArrayList<Label>(n);
String title=in.nextLine();
Label newLabel=new Label(title,"",1,null);
list.add(newLabel);
parents.add(newLabel);
for(int i=1;i<n;i++) {
String str=in.nextLine();
String[] parts=str.split(" ");
title=parts[0].substring(parts[0].lastIndexOf(".")+1).toLowerCase();
String id=parts.length==2?parts[1]:"";
newLabel=new Label(title,id,i+1,parents.get((str.lastIndexOf(".")+1)/2-1));
list.add(newLabel);
if(parents.size()==(str.lastIndexOf(".")+1)/2) {
parents.add(newLabel);
}else {
parents.set((str.lastIndexOf(".")+1)/2, newLabel);
}
}
String css[][]=new String[m][];
for(int i=0;i<m;i++) {
String parts[]=in.nextLine().split(" ");
for(int j=0;j<parts.length;j++) {
if(parts[j].charAt(0)!='#') {
parts[j]=parts[j].toLowerCase();
}
}
css[i]=parts;
}
for(int i=0;i<m;i++) {
ArrayList<Integer> lines=new ArrayList<Integer>();
for(int j=0;j<list.size();j++) {
int index=css[i].length-1;
if(list.get(j).title.equals(css[i][index])||list.get(j).id.equals(css[i][index])) {
Label temp=list.get(j).parent;
index--;
while(temp!=null&&index>=0) {
if(temp.title.equals(css[i][index])||temp.id.equals(css[i][index])) {
index--;
}
temp=temp.parent;
}
if(index==-1)
lines.add(list.get(j).line);
}
}
Collections.sort(lines);//还需对行号排序
System.out.print(lines.size());
for(int j=0;j<lines.size();j++) {
System.out.print(" "+lines.get(j));
}
System.out.println();
}
}
}
class Label{
String title="";
String id="";
int line=0;
Label parent=null;
public Label(String title,String id,int line,Label parent) {
this.title=title;
this.id=id;
this.line=line;
this.parent=parent;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)